XSD: What is the root element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sascha Kerschhofer

    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


  • Steve Jorgensen

    #2
    Re: XSD: What is the root element

    On Tue, 16 Aug 2005 20:41:16 +0200, "Sascha Kerschhofer"
    <skerschhofer@y ahoo.antispam.d e> wrote:
    [color=blue]
    >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?[/color]

    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.

    Comment

    • Henry S. Thompson

      #3
      Re: XSD: What is the root element

      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]

      Comment

      • Steve Jorgensen

        #4
        Re: XSD: What is the root element

        On Wed, 17 Aug 2005 13:22:17 +0100, ht@inf.ed.ac.uk (Henry S. Thompson) wrote:
        [color=blue]
        >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[/color]

        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.

        Comment

        Working...