Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema: root element

Tags:

xml

xsd

The following post asks how to indicate that an element is the root element in an XML schema:

Is it possible to define a root element in an XML Document using Schema?

I have followed the w3schools tutorial on XML Schema but something is still not clear. Consider example schema 2 from https://www.w3schools.com/xml/schema_example.asp (reproduced below for convenience). How does this code indicate that <shiporder> is the root element? Isn't the example saying that all elements are valid as root elements?

------------------ instance ----------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>  <shiporder orderid="889923"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="shiporder.xsd">   <orderperson>John Smith</orderperson>   <shipto>     <name>Ola Nordmann</name>     <address>Langgt 23</address>     <city>4000 Stavanger</city>     <country>Norway</country>   </shipto>   <item>     <title>Empire Burlesque</title>     <note>Special Edition</note>     <quantity>1</quantity>     <price>10.90</price>   </item>   <item>     <title>Hide your heart</title>     <quantity>1</xample saying that all elements are valid as root elements?quantity>     <price>9.90</price>   </item> </shiporder>  

----------------------- schema ------------------------

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

From my point of view an XML Schema should do two things:

  1. define what can occur inside each node
  2. define where each node can be placed

And it seems the example fails at #2. Any Suggestions?

like image 425
johngoche9999 Avatar asked Jan 13 '12 16:01

johngoche9999


People also ask

How do I find the root element in XML?

In any markup language, the first element to appear is called the "root element", which defines what kind of document the file will be. In an HTML file, the <html> tag is the root element.

Can XML document have root element?

All XML documents must have a root element All XML documents must contain a single tag pair to define a root element. All other elements must be within this root element. All elements can have sub elements (child elements). Sub elements must be correctly nested within their parent element.

How many root elements are in XML?

While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots. If one of the roots matches that in the XML source file, that root element is used, otherwise you need to select one to use. Choose the root to display. Select the root element you want to use.

What are root nodes in XML?

The root node is the parent of all other nodes in the document. An immediate descendant of another node. Note that element attributes are not generally considered child elements.


1 Answers

As far as I know, any globally defined element can be used as root element, and XML Schema does not have a notion for specifying what the root element is supposed to be.

You can however work around this by designing your XML Schema well, so that there is only one globally defined element - then only this element is valid as root element.

An example of this can be found at W3Schools (heading Using Named Types) This example only has one globally defined element, and thus only one possible root element.

like image 166
Has QUIT--Anony-Mousse Avatar answered Sep 25 '22 07:09

Has QUIT--Anony-Mousse