4

I am making a school assignment This is my XML

<lineup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="ComplexTypeDemo.xsd">
  <team teamName="Maple Leafs" city="Toronto">
    <visitor/>
      <player number="17">
        <name>John Doe</name>
        <position>Forward</position>
      </player>
      <!--Continue 20 Iterations-->
    </team>
    <team teamName="Rangers" city="New York">
     <home/>
      <player number="17">
        <name>John Doe</name>
        <position>Forward</position>
      </player>
      <!--Continue 20 Iterations-->
    </team>
</lineup>

here is my schema document

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="lineup">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="team" minOccurs="2" maxOccurs="2">

          <xs:complexType>
            <xs:sequence>


            <xs:choice>
              <xs:element name="home"/>
              <xs:element name="visitor"/>
            </xs:choice>

              <xs:element name="player" minOccurs="20" maxOccurs="20">
                <xs:complexType>
                  <xs:all>
                    <xs:element name="name" />
                    <xs:element name="position"/>
                  </xs:all>
                </xs:complexType>

              </xs:element>

            </xs:sequence>
          </xs:complexType>


        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

i need to make a schema to validate this. but i can't figure out how to validate because it is nested but it has attributes. I can only seem to do one or the other, but not both....

2 Answers 2

4

You are close. You can place attribute declarations after the xs:sequence or xs:all close tags but before the xs:complexType close tag. This update to your XSD will validate the XML document instance you provide (modulo actually including 20 iterations as noted):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="lineup">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="team" minOccurs="2" maxOccurs="2">
          <xs:complexType>
            <xs:sequence>
              <xs:choice>
                <xs:element name="home"/>
                <xs:element name="visitor"/>
              </xs:choice>
              <xs:element name="player" minOccurs="20" maxOccurs="20">
                <xs:complexType>
                  <xs:all>
                    <xs:element name="name" />
                    <xs:element name="position"/>
                  </xs:all>
                  <xs:attribute name="number" type="xs:integer"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="teamName"/>
            <xs:attribute name="city"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Sign up to request clarification or add additional context in comments.

1 Comment

oh thank you very much. I think i tried every combination of <xs:whatever> but i didn't realize that the attributes need to go at the end of the nested portion.
2

If you have a complex type, you should place the attribute references (or declarations) after your groups (sequences, choices, etc.). For example:

<xs:element name="team">
    <xs:complexType>
        <xs:sequence>
            <xs:choice>
                <xs:element ref="home"/>
                <xs:element ref="visitor"/>
            </xs:choice>
            <xs:element ref="player" minOccurs="20" maxOccurs="20"/>
        </xs:sequence>
        <xs:attribute name="city" type="xs:string"/>
        <xs:attribute name="teamName" type="xs:string"/>
    </xs:complexType>
</xs:element>

If you have a simple type (for example, an empty element which requires an attribute, or one that has string contents), then you should declare your type as a complex type with simple content, and provide an extension of the simple content for your attributes. For example:

<xs:element name="visitor">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="visitorName" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

1 Comment

Correct too and +1 for also showing xs:simpleContent example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.