xmlxsd

A cleaner way of writing an XSD document


Is there a cleaner way of writing a XSD document. I find that the code is very difficult to read whene there is lots of attributes and extended elements. This is my XSD example:

I added a few restriction on attributes.

?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="footballGame">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="game">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="team" minOccurs="2" maxOccurs="2">
                <xs:complexType>
                  <xs:sequence>
                  <xs:element name="teamName" type="xs:string"/>
                  <xs:element name="goal" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="playerName" type="xs:string"/>
                        <xs:element name="goalMinute">
                          <xs:simpleType>
                            <xs:restriction base="xs:string">
                              <xs:length value="2"/>
                            </xs:restriction>
                          </xs:simpleType>
                        </xs:element>
                      </xs:sequence> 
                    <xs:attribute name="penalty" use="required">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:enumeration value="yes"/>
                          <xs:enumeration value="no"/> 
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:attribute>
                    </xs:complexType> 
                  </xs:element>
                  <xs:element name="card" minOccurs="0" maxOccurs="unbounded">
                  <xs:complexType>
                  <xs:sequence>
                    <xs:element name="playerNmae" type="xs:string"/>
                    <xs:element name="cardType">
                     <xs:complexType>
                      <xs:attribute name="card"> 
                        <xs:simpleType>
                          <xs:restriction base="xs:string">
                            <xs:enumeration value="yellow"/>
                            <xs:enumeration value="red"/>
                          </xs:restriction> 
                        </xs:simpleType>
                      </xs:attribute>
                     </xs:complexType> 
                    </xs:element>
                    <xs:element name="cardMinute" type="xs:positiveInteger"/>
                </xs:sequence>              
              </xs:complexType>                  
                  </xs:element>
                  </xs:sequence>
                  <xs:attribute name="homeOrAway" use="required">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:enumeration value="home"/>
                        <xs:enumeration value="away"/> 
                      </xs:restriction> 
                    </xs:simpleType> 
                  </xs:attribute>
                </xs:complexType> 
              </xs:element> 
            </xs:sequence> 
          </xs:complexType> 
        </xs:element> 
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I tried putting attributes to the end of code, but dont know how to reference them.


Solution

  • You could define the types for your element and attributes separately, and then declare that elements and attributes are of that type:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="footballGame" type="footballGame" />
           
        <xs:complexType name="footballGame">
            <xs:sequence>
                <xs:element type="game" name="game" />
            </xs:sequence>
        </xs:complexType>
        
        <xs:complexType name="game">
            <xs:sequence>
                <xs:element type="team" name="team" minOccurs="2" maxOccurs="2"/>
            </xs:sequence> 
        </xs:complexType>
        
        <xs:complexType name="team">
            <xs:sequence>
                <xs:element name="teamName" type="xs:string"/>
                <xs:element type="goal" name="goal" minOccurs="0" maxOccurs="unbounded" />
                <xs:element type="card" name="card" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute type="homeOrAway" name="homeOrAway" use="required" />
        </xs:complexType>
        
        <xs:simpleType name="homeOrAway">
            <xs:restriction base="xs:string">
                <xs:enumeration value="home"/>
                <xs:enumeration value="away"/> 
            </xs:restriction> 
        </xs:simpleType>
        
        <xs:complexType name="goal">
            <xs:sequence>
                <xs:element name="playerName" type="xs:string"/>
                <xs:element type="minute" name="goalMinute" />
            </xs:sequence> 
            <xs:attribute type="penalty" name="penalty" use="required"/>
        </xs:complexType>
        
        <xs:simpleType name="minute">
            <xs:restriction base="xs:string">
                <xs:length value="2"/>
            </xs:restriction>
        </xs:simpleType>
        
        <xs:simpleType name="penalty">
            <xs:restriction base="xs:string">
                <xs:enumeration value="yes"/>
                <xs:enumeration value="no"/> 
            </xs:restriction>
        </xs:simpleType>
        
        <xs:complexType name="card">
            <xs:sequence>
                <xs:element name="playerNmae" type="xs:string"/>
                <xs:element name="cardType">
                    <xs:complexType>
                        <xs:attribute name="card"> 
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="yellow"/>
                                    <xs:enumeration value="red"/>
                                </xs:restriction> 
                            </xs:simpleType>
                        </xs:attribute>
                    </xs:complexType> 
                </xs:element>
                <xs:element name="cardMinute" type="xs:positiveInteger"/>
            </xs:sequence>              
        </xs:complexType>
        
    </xs:schema>
    

    See also