XSD: What is the root element

Discussion in 'XML' started by Sascha Kerschhofer, Aug 16, 2005.

  1. 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
     
    Sascha Kerschhofer, Aug 16, 2005
    #1
    1. Advertising

  2. On Tue, 16 Aug 2005 20:41:16 +0200, "Sascha Kerschhofer"
    <> 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.
     
    Steve Jorgensen, Aug 17, 2005
    #2
    1. Advertising

  3. 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:
    URL: http://www.ltg.ed.ac.uk/~ht/
    [mail really from me _always_ has this .sig -- mail without it is forged spam]
     
    Henry S. Thompson, Aug 17, 2005
    #3
  4. On Wed, 17 Aug 2005 13:22:17 +0100, (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.
     
    Steve Jorgensen, Aug 17, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,236
  2. Matthet
    Replies:
    2
    Views:
    671
    Matthet
    Sep 24, 2003
  3. VK
    Replies:
    8
    Views:
    508
    Joseph Kesselman
    Oct 31, 2006
  4. Andrew Thompson
    Replies:
    1
    Views:
    872
    Andrew Thompson
    Sep 24, 2007
  5. PeteOlcott
    Replies:
    1
    Views:
    948
    Mukul Gandhi
    Feb 5, 2009
Loading...

Share This Page