XSD: What is the root element

S

Sascha Kerschhofer

If I define more than one element "globally" in an XML schema, is there any
hint which one is the actual root element for any instance document?
e.g.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element ref="b"/>
<xs:element name="c"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b"/>
</xs:schema>

allows two valid instance documents. First is

<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>Text</b>
<c>Text</c>
</a>

Second is simply
<?xml version="1.0" encoding="UTF-8"?>
<b/>

Of course a is meant as root element but I want to use global elements for
reuse however. How can I avoid this ambiguousity?

Thank you.
Sascha
 
S

Steve Jorgensen

If I define more than one element "globally" in an XML schema, is there any
hint which one is the actual root element for any instance document?
e.g.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element ref="b"/>
<xs:element name="c"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b"/>
</xs:schema>

allows two valid instance documents. First is

<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>Text</b>
<c>Text</c>
</a>

Second is simply
<?xml version="1.0" encoding="UTF-8"?>
<b/>

Of course a is meant as root element but I want to use global elements for
reuse however. How can I avoid this ambiguousity?

There's no way in an xsd to say which globally defined element or elements may
or may not be the root element. Many reputable people say that, for that
reason you should use types to slice up the design layers, and not define
multiple global elements. Personally, I disagree - I think defining many
global elements in an xsd is the best design approach, in spite of the root
element problem.

What OASIS says on the subject is to add a documentation annotation to the
root element definition stating that it is the one and only element for use as
a root. I also like to make an annotation at the top, schema level stating
that there is only one root element, and says which element that is.
 
H

Henry S. Thompson

Some schema processors, including XSV [1], take a command line or
invocation argument allowing you to specify a name you want to be
required for the document element.

ht

[1] http://www.ltg.ed.ac.uk/~ht/xsv-status.html
--
Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
Half-time member of W3C Team
2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
Fax: (44) 131 650-4587, e-mail: (e-mail address removed)
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
 
S

Steve Jorgensen

Some schema processors, including XSV [1], take a command line or
invocation argument allowing you to specify a name you want to be
required for the document element.

ht

[1] http://www.ltg.ed.ac.uk/~ht/xsv-status.html

I'm not sure how helpful that is vs having the application check the root
element name after parsing. It saves taking the time to fully invalidate and
load a useless document, but it won't stop an author from using your schema to
validate that same document successfully.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top