XML Schema element validation order

This should cover W3C XML Schema, Relax NG and DTD related problems.
mirceaa
Posts: 1
Joined: Thu Apr 12, 2007 1:29 pm

XML Schema element validation order

Post by mirceaa »

Hello,

I'm following the example in the XML schema tutorial from the w3.org, however I can't understand the design method based on defining all elements and attributes first, and then referring to them using the ref attribute:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes
-->
<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element
ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>


The question is how can I identify the desired root element of the target document? I can see from the example it's the "shiporder" element, but why not the "orderto" or "shipto" or "item" elements that are previously defined ?

Thank you !
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

As you noticed XML Schema allows any global element to be a document root element for a document that can be valid against the schema. There is no mechanism in XML Schema to say that one or more elements are the elements that should be rott elements.

As a side note, Relax NG has a start pattern that defines what elements can be the root elements for valid documents.

Best Regards,
George
George Cristian Bina
Post Reply