Root element

Build 1501 on 14/Nov/2017  This topic last edited on: 25/Aug/2014, at 10:33

The exception to the Rules for defining complex types is the ‘root element’, which is a special element identified by the fact that its type is not derived, and that is used to declare the structure of XML files containing multiple objects. In other words, if the schema contained only the declaration of the elements corresponding to object types – e.g., only ‘test’ in the example above, an XML file matching the schema will always have this structure shown below, and it will not be possible to create XML files matching the schema containing multiple objects.

<?xml version="1.0" encoding="utf-8"?>

<test location="Milan" >

</test>

 

The solution is to declare the root element in the schema like this:

<xs:element name="objects">

   <xs:complexType>

     <xs:sequence minOccurs="0" maxOccurs="unbounded">

       <xs:element ref="test" minOccurs="0" maxOccurs="unbounded"/>

     </xs:sequence>

   </xs:complexType>

 </xs:element>

 

This lists the various elements (corresponding to object types) that can be put together in a single XML file, so that it is possible to have a valid XML file such as:

<?xml version="1.0" encoding="utf-8"?>

<objects>

 <test location="Milan">

 </test>

 <test location="Paris">

 </test>

</objects>

 

The root element can have any name (‘objects’ being the standard one), because it is identified by the fact that it has a complex type that is not derived.

IMPORTANT: There must be one and only one root element in the schema.

Last but not least, the root element declaration has an impact only on the structure of the valid import/export XML files, it does not have any effect on the structure of the database (i.e. ,the available object types and their structure).