XML验证问题(S4s-elt-invalid-content.1)

问题描述 投票:1回答:1

我从验证器收到此错误:

S4s-elt-invalid-content.1:'schema'的内容无效。元素'键'无效,错位或太频繁发生。

如果我删除键/ keyref它可以正常工作但如果我包含它则不行。我无法弄清楚错误是什么,谷歌搜索和搜索在这里没有显示任何可以解决此密钥/参考问题。任何帮助将非常感激。

.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<hwmarketing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
    <campaigns>
        <campaign campID="1">
            <releaseDate>05/25/2018</releaseDate>
            <endDate>09/22/2018</endDate>
            <advertisments adRef="1" />
        </campaign>
    </campaigns>
    <adverts>
        <magadvert advertID="1">
            <releaseDate>03/10/2019</releaseDate>
            <endDate>01/01/2020</endDate>
            <magName>Laoreet Ipsum Curabitur Ltd</magName>
            <advertSize>671.69</advertSize>
            <position>Inside front</position>
            <magCost>696233.37</magCost>
            <issuesAppeared>34</issuesAppeared>
        </magadvert>
    </adverts>
    <employees>
        <employee empID="10">
            <fName>Zorita</fName>
            <lName>William</lName>
            <DOB>02/16/2020</DOB>
            <workedOn campaign="1" />
        </employee>
    </employees>
    <noOfHours>
        <employeeHours>
            <hours>1237</hours>
            <campaignEmployee campID="10" empID="10" />
        </employeeHours>
    </noOfHours>
</hwmarketing>

schema.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Root element -->
<xs:element name="hwmarketing" />
<!-- Position Datatype -->
<xs:simpleType name="position">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Inside front"/>
        <xs:enumeration value="Inside back"/>
        <xs:enumeration value="Other"/>
    </xs:restriction>
</xs:simpleType>
<!-- Custom datatype for Date formatting -->
<xs:simpleType name="dateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value="\d{2}[/]\d{2}[/]\d{4}"></xs:pattern>
        <xs:length value="10"/>
    </xs:restriction>
</xs:simpleType>
<!-- Currency datatype -->
<xs:simpleType name="currency">
    <xs:restriction base="xs:decimal">
        <xs:minInclusive value="100"/>
        <xs:maxInclusive value="999999"/>
        <xs:totalDigits value="2"/>
    </xs:restriction>
</xs:simpleType>
<!-- Campaign element -->
<xs:element name="campaign" >
    <xs:complexType>
        <xs:sequence>
            <xs:element name="releaseDate" type="dateFormat"/>
            <xs:element name="endDate" type="dateFormat"/>
            <xs:element name="advertisments" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="campID" type="xs:positiveInteger" use="required"/>
    </xs:complexType>
</xs:element>
<!-- Magazine element -->
<xs:element name="magAdvert" >
    <xs:complexType>
        <xs:sequence>
            <xs:element name="releaseDate" type="dateFormat"/>
            <xs:element name="endDate" type="dateFormat"/>
            <xs:element name="magName" type="xs:string"/>
            <xs:element name="advertSize" type="xs:positiveInteger"/>
            <xs:element name="position" type="position"/>
            <xs:element name="issuesAppeared" type="xs:positiveInteger"/>
            <xs:element name="magCost" type="currency"/>
        </xs:sequence>
        <xs:attribute name="advertID" type="xs:positiveInteger" use="required"/>
    </xs:complexType>
</xs:element>
<!-- Employee element -->
<xs:element name="employee" >
    <xs:complexType>
        <xs:sequence>
            <xs:element name="fName" type="xs:string"/>
            <xs:element name="lName" type="xs:string"/>
            <xs:element name="DOB" type="dateFormat"/>
            <xs:element name="workedOn" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="empID" type="xs:positiveInteger" use="required"/>
    </xs:complexType>
</xs:element>
<!-- noOfHours element -->
<xs:element name="employeeHours">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="hours" type="xs:positiveInteger" />
            <xs:element name="campaignEmployee" minOccurs="1" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:key name="adKey">
    <xs:selector xpath="hwmarketing/adverts/magAdvert" />
    <xs:field xpath="@advertID"/>
</xs:key>
<xs:keyref name="adRef" ref="adKey">
    <xs:selector xpath="hwmarketing/campaigns/campaign" />
    <xs:field xpath="@advertisment"/>
</xs:keyref>
</xs:schema>
xml validation xsd xsd-validation
1个回答
2
投票

您的XSD部分似乎缺失,可能是由于努力创建最小的完整示例。但是,您的错误的直接原因是xs:keyxs:keyref元素不能是xs:schema元素的子元素。

把它们放在xs:element元素的孩子们的最后(在任何xs:simpleTypexs:complexType之后,在</xs:element>之前)。然后将XPath调整到新的上下文。

© www.soinside.com 2019 - 2024. All rights reserved.