TKG
TKG

Reputation: 21

XSD content of element declaration must match; suspect attributes

I'm having difficulty generating an XSD that allows me to validate an XML. My current error is Error at Line 9: Column 47, content of element declaration must match (annotation?,(simpleType|complexType)?,(unique|key|keyref)*). I have checked the well-formedness of the XML and XSD; according to my editor they are.

I'm assuming the error refers to the XSD rather than the XML instance, mainly because I don't have a column 47 at line 9 in my XML. Btw, the XML cannot be altered. This is part of an assignment.

I have tried a few things to get the error to go away. I did once (forget how), but I got another one in its' place at pretty much the same location in my XSD (that concerned an element having both an attribute type and a simple|complexType). I've tried moving the attribute to just before the complexType closing tag that closes out that element (so after all my child elements and I've closed the sequence tag).I've tried putting it at the beginning nested in complexType tags.

Basically I'm going in a circle, and am hoping someone can point the way out. I suspect it has something to do with how I'm assigning attribute values to my elements, particularly in the case of the UpdatedBy element, which has an attribute, it's own text string separate from the attribute and no children.

The XML Code is:

    <?xml version="1.0" encoding="UTF-8"?>
    <task
       xmlns="http://www.example.com/task"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.example.com/task task2.xsd">
        <UpdatedBy kind="SalesPerson">Lisa Moroz</UpdatedBy>
        <ShipTo country="Canada">
          <name>Maya Wells</name>
          <street>44 Gamble Avenue</street>
          <city>Toronto</city>
          <province>ON</province>
          <zip>L1M3G1</zip>
        </ShipTo>
        <ShipTo country="Canada">
          <name>Maya Wells</name>
          <street>62 Elm Street</street>
          <city>Montreal</city>
          <province>QC</province>
          <zip>K2J3H4</zip>
        </ShipTo>
        <BillTo>
          <country>Canada</country>
          <name>Maya Wells</name>
          <street>78 Audley Street</street>
          <city>Oakville</city>
          <province>ON</province>
          <zip>O3R1M5</zip>
        </BillTo>
      </task>

Here's the XSD:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:target="http://www.example.com/task"
    targetNamespace="http://www.example.com/task" elementFormDefault="qualified">
        <element name="name">
           <complexType>
              <sequence>
                 <element name="UpdatedBy" type="string">
                    <attribute name="kind" type="string"/>
                 </element>
                 <element name="ShipTo" maxOccurs="4">
                    <complexType>
                       <sequence>
                          <element name="name" type="string"/>
                             <simpleType>
                                <element name="street" type="string">
                                   <restriction base="string">
                                     <minLength value="5"/>
                                   </restriction>
                                </element>
                             </simpleType>
                         <element name="city" type="string"/> 
                             <simpleType>
                               <element name="province" type="string">
                                   <restriction base="string">
                                      <enumeration value="AB"/>
                                      <enumeration value="BC"/>
                                      <enumeration value="MB"/>
                                      <enumeration value="NB"/>
                                      <enumeration value="NL"/>
                                      <enumeration value="NS"/>
                                      <enumeration value="NU"/>
                                      <enumeration value="ON"/>
                                      <enumeration value="PE"/>
                                      <enumeration value="QC"/>
                                      <enumeration value="SK"/>
                                      <enumeration value="YT"/>
                                   </restriction>
                               </element>
                           </simpleType>
                        <element name="zip" type="string"/>
                      </sequence> 
                    <attribute name="country" type="string"/>
                 </complexType>
            </element>
            <element name="BillTo" minOccurs="1">
                 <complexType>
                    <sequence>
                       <element name="country" type="string"/>
                       <element name="name" type="string"/>
                         <simpleType>
                            <element name="street" type="string">
                               <restriction base="string">
                                 <minLength value="5"/>
                               </restriction>
                            </element>
                         </simpleType>
                       <element name="city" type="string"/>
                         <simpleType>
                            <element name="province" type="string">
                                <restriction base="string">
                                   <enumeration value="AB"/>
                                   <enumeration value="BC"/>
                                   <enumeration value="MB"/>
                                   <enumeration value="NB"/>
                                   <enumeration value="NL"/>
                                   <enumeration value="NS"/>
                                   <enumeration value="NU"/>
                                   <enumeration value="ON"/>
                                   <enumeration value="PE"/>
                                   <enumeration value="QC"/>
                                   <enumeration value="SK"/>
                                   <enumeration value="YT"/>
                                </restriction>
                           </element>
                        </simpleType>
                     <element name="zip" type="string"/>
                  </sequence> 
              </complexType>
              </element>
            </sequence>
          </complexType>
        </element>
    </schema>

Thanks for any tips and suggestions :) [Either on the question or how I can present my questions better in the future; first posting]

Upvotes: 2

Views: 2817

Answers (1)

kjhughes
kjhughes

Reputation: 111541

Required Changes

(1) Change

<element name="name">

to

<element name="task">

(2) Declarations like the following,

    <element name="UpdatedBy" type="string">
      <attribute name="kind" type="string"/>
    </element>

require an xsd:complexType and no @type="string" attribute:

    <element name="UpdatedBy">
      <complexType>
        <simpleContent>
          <extension base="string">
            <attribute name="kind" type="string"/>
          </extension>
        </simpleContent>
      </complexType>
    </element>

(3) Push xsd:simpleType beneath xsd:element in several places in the XSD.

After making the above changes, your XML document:

<?xml version="1.0" encoding="UTF-8"?>
<task
    xmlns="http://www.example.com/task"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.com/task try.xsd">
  <UpdatedBy kind="SalesPerson">Lisa Moroz</UpdatedBy>
  <ShipTo country="Canada">
    <name>Maya Wells</name>
    <street>44 Gamble Avenue</street>
    <city>Toronto</city>
    <province>ON</province>
    <zip>L1M3G1</zip>
  </ShipTo>
  <ShipTo country="Canada">
    <name>Maya Wells</name>
    <street>62 Elm Street</street>
    <city>Montreal</city>
    <province>QC</province>
    <zip>K2J3H4</zip>
  </ShipTo>
  <BillTo>
    <country>Canada</country>
    <name>Maya Wells</name>
    <street>78 Audley Street</street>
    <city>Oakville</city>
    <province>ON</province>
    <zip>O3R1M5</zip>
  </BillTo>
</task>

Will be valid against this updated XSD:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:target="http://www.example.com/task"
        targetNamespace="http://www.example.com/task" elementFormDefault="qualified">
  <element name="task">
    <complexType>
      <sequence>
        <element name="UpdatedBy">
          <complexType>
            <simpleContent>
              <extension base="string">
                <attribute name="kind" type="string"/>
              </extension>
            </simpleContent>
          </complexType>
        </element>
        <element name="ShipTo" maxOccurs="4">
          <complexType>
            <sequence>
              <element name="name" type="string"/>
              <element name="street">
                <simpleType>
                  <restriction base="string">
                    <minLength value="5"/>
                  </restriction>
                </simpleType>
              </element>
              <element name="city" type="string"/> 
              <element name="province">
                <simpleType>
                  <restriction base="string">
                    <enumeration value="AB"/>
                    <enumeration value="BC"/>
                    <enumeration value="MB"/>
                    <enumeration value="NB"/>
                    <enumeration value="NL"/>
                    <enumeration value="NS"/>
                    <enumeration value="NU"/>
                    <enumeration value="ON"/>
                    <enumeration value="PE"/>
                    <enumeration value="QC"/>
                    <enumeration value="SK"/>
                    <enumeration value="YT"/>
                  </restriction>
                </simpleType>
              </element>
              <element name="zip" type="string"/>
            </sequence> 
            <attribute name="country" type="string"/>
          </complexType>
        </element>
        <element name="BillTo" minOccurs="1">
          <complexType>
            <sequence>
              <element name="country" type="string"/>
              <element name="name" type="string"/>
              <element name="street">
                <simpleType>
                  <restriction base="string">
                    <minLength value="5"/>
                  </restriction>
                </simpleType>
              </element>
              <element name="city" type="string"/>
              <element name="province">
                <simpleType>
                  <restriction base="string">
                    <enumeration value="AB"/>
                    <enumeration value="BC"/>
                    <enumeration value="MB"/>
                    <enumeration value="NB"/>
                    <enumeration value="NL"/>
                    <enumeration value="NS"/>
                    <enumeration value="NU"/>
                    <enumeration value="ON"/>
                    <enumeration value="PE"/>
                    <enumeration value="QC"/>
                    <enumeration value="SK"/>
                    <enumeration value="YT"/>
                  </restriction>
                </simpleType>
              </element>
              <element name="zip" type="string"/>
            </sequence> 
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

Upvotes: 1

Related Questions