471,344 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

XSD: What is the root element

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
Aug 16 '05 #1
3 3095
On Tue, 16 Aug 2005 20:41:16 +0200, "Sascha Kerschhofer"
<sk**********@yahoo.antispam.de> wrote:
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.
Aug 17 '05 #2
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: ht@inf.ed.ac.uk
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
Aug 17 '05 #3
On Wed, 17 Aug 2005 13:22:17 +0100, ht@inf.ed.ac.uk (Henry S. Thompson) wrote:
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.
Aug 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by yaya9 | last post: by
9 posts views Thread by Eskimo | last post: by
2 posts views Thread by PeterW | last post: by
2 posts views Thread by farseer | last post: by
4 posts views Thread by Jonas Bush | last post: by
reply views Thread by Dave Hill | last post: by
2 posts views Thread by =?Utf-8?B?TWlnaHR5IE1hcms=?= | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.