Ingó Vals
Ingó Vals

Reputation: 4898

XML Schema declare a root

If I have a schema like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema  targetNamespace="http://mysticwarlords.kaa/XMLSchema"
            xmlns="http://mysticwarlords.kaa/XMLSchema"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="titletype">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Warlord"/>
            <xs:enumeration value="FirstMate"/>
            <xs:enumeration value="Jester"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="warlord">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="warlordName" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="title" type="titletype" />
        </xs:complexType>
    </xs:element>

    <xs:element name="warband">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="warbandName" type="xs:string" />
            <xs:element name="warlords">
              <xs:complexType>
                <xs:sequence>
                  <xs:element ref="warlord" minOccurs="1" maxOccurs="unbounded">
                    <xs:unique name="eachTitleOnlyOnce">
                        <xs:selector xpath="warlord"/>
                        <xs:field xpath="@title"/>
                    </xs:unique>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="wargroup">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="wargroupName" type="xs:string" />
            <xs:element name="warlords">
              <xs:complexType>
                <xs:sequence>
                  <xs:element ref="warlord" minOccurs="1" maxOccurs="10" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="war">
        <xs:complexType>
            <xs:choice>
              <xs:element ref="wargroup"/>
              <xs:element ref="warband"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>

</xs:schema>

I guess it would validate a xml file that is only a element?

What if I only wanted to allow to be the root element?

Upvotes: 1

Views: 894

Answers (1)

MiMo
MiMo

Reputation: 11953

Your schema will validate XML files containing a root element warlord or warband or wargroup or war.

If I understand your question correctly what you want is to validate only XML files that contain the root element war. To do that you should modify the definitions of warlord, warband and wargroup to be named complex types - e.g.:

  <xs:complexType name="warlord"> 
      <xs:sequence> 
          <xs:element name="warlordName" type="xs:string"/> 
      </xs:sequence> 
      <xs:attribute name="title" type="titletype" /> 
  </xs:complexType> 

and then use this types instead than ref - e.g.:

<xs:element name="war">   
    <xs:complexType>   
        <xs:choice>   
          <xs:element name="wargroup" type="wargroup"/>   
          <xs:element name="warband" type="warband"/>   
        </xs:choice>   
    </xs:complexType>   
</xs:element>   

Upvotes: 1

Related Questions