Arnaud Courtecuisse
Arnaud Courtecuisse

Reputation: 367

What is the XSD representation of an element with attributes and subelements?

My XML looks like this :

<root>
  <elt att1="bla" att2="ble">
    <subelt satt1="bli" satt2="1" />
    <subelt satt1="blo" satt2="18" />
    <subelt satt1="blu" satt2="4" />
  </elt>
</root>

There could be 1 to any elt. There could be 1 to any subelt by elt. I try to validate my schema against this utility : http://www.utilities-online.info/xsdvalidation. Here is my xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="elt" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="att1" type="xs:string" use="required"/>
            <xs:attribute name="att2" type="xs:string" use="required"/>
            <xs:sequence>
              <xs:element name="subelt" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:attribute name="subatt1" type="xs:string" use="required"/>
                  <xs:attribute name="subatt2" type="xs:integer"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The problem comes from the element with both attributes and sub-elements (sequence). I tried putting a xs:complexType around sequence, and still get a different error. I'm a beginner with XML schemas, so it is a little difficult for me to understand how I should format this. Could you give me some advice about it ? Thanks.

(There might be some typos in my files, since I changed elements and attributes names)

Upvotes: 0

Views: 100

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25054

For historical reasons, XSD complex types specify the content model of the element first, then the attributes. So the complex type of the elt element should look something like:

<xs:complexType>
  <xs:sequence>
    <xs:element name="subelt" minOccurs="1" maxOccurs="unbounded">
      <xs:complexType>
        <xs:attribute name="subatt1" type="xs:string" use="required"/>
        <xs:attribute name="subatt2" type="xs:integer"/>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
  <xs:attribute name="att1" type="xs:string" use="required"/>
  <xs:attribute name="att2" type="xs:string" use="required"/>
</xs:complexType>

Upvotes: 1

Related Questions