xml schema nested element within an element with attributes

xml, xsd

Solution

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>

Problem

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

Original source