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

时间:2019-03-19 15:23:48

标签: xml validation xsd xsd-validation

我从验证器收到此错误:

  

S4s-elt-invalid-content.1:“模式”的内容无效。元素“键”无效,放错位置或经常发生。

如果删除键/ 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>

1 个答案:

答案 0 :(得分:2)

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

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