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

XML Validation Issue (S4s-elt-invalid-content.1)

我从验证程序中收到此错误:

S4s-elt-invalid-content.1: The Content Of 'schema' Is Invalid. Element 'key' Is Invalid, Misplaced, Or Occurs Too Often.

如果我删除 key/keyref 它可以正常工作,但如果我包含它则不会。我无法弄清楚可能是什么错误,google 搜索和此处搜索没有显示任何可以解决此 key/ref 问题的内容。非常感谢任何帮助。

.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>

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

将它们作为 xs:element 元素的子元素放在最后(在 xs:simpleTypexs:complexType 之后,就在 </xs:element> 之前)。然后根据新上下文调整 XPath。