Naeem Khan
Naeem Khan

Reputation: 960

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

I'm getting this error from validator:

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

It works fine if I remove the key/keyref but doesn't if I include it. I cannot figure out what the error could be and google search and searching here has not shown anything that can fix this key/ref issue. Any help will be really appreciated.

.xml file:

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

Upvotes: 1

Views: 938

Answers (1)

kjhughes
kjhughes

Reputation: 111620

Parts of your XSD appear to be missing, perhaps due to efforts to create a minimal complete example. However, the direct cause of your error is that the xs:key and xs:keyref elements cannot be children of the xs:schema element.

Place them instead as children of an xs:element element, at the end (after any xs:simpleType or xs:complexType, just ahead of </xs:element>). Then adjust the XPaths to that new context.

Upvotes: 2

Related Questions