Schema for validating unordered sequences?

Discussion in 'XML' started by Kevin Campbell, Apr 22, 2004.

  1. Say I have the following XML spec:

    <Book>
    <Title />
    <Author />
    <ISBN />
    <Description />
    </Book>

    Title, Author, ISBN and Description may appear in any order. Title,
    Author and ISBN must appear exactly once, and Description may appear 0
    or 1 times. Thus, the following XML is also valid.

    <Book>
    <Author />
    <ISBN />
    <Title />
    </Book>

    I haven't been working with XML schemas for very long, but from what I
    have read it seems that it would be fairly difficult to validate this
    type of XML using schemas.

    I know how to specify an unordered list where all elements are
    optional and unbounded:

    <xs:complexType name="BookType">
    <xs:choice maxOccurs="unbounded">
    <xs:element ref="Title"/>
    <xs:element ref="Author"/>
    <xs:element ref="ISBN"/>
    <xs:element ref="Description"/>
    </xs:choice>
    </xs:complexType>
    <xs:element name="Book" type="BookType" />

    However, such a schema permits invalid XML such as this:

    <Book>
    <Title>
    <Title>
    </Book>

    Using the above schema, I could perform further validation in my code,
    but I am wondering if there is a way to do it using schema only. It
    seems to me that one of the nice benefits of XML as a structured data
    format is that element ordering is not necessarily relevant. In my
    example the order of the elements has no semantic meaning, so I would
    like authors to be able to not worry about ordering while still
    gaining the benefits of validated XML.

    Does anyone know how to accomplish this using XML Schemas?

    Thanks,
    Kevin Campbell
    Kevin Campbell, Apr 22, 2004
    #1
    1. Advertising

  2. Kevin,

    Use xs:all instead of xs:choice. That should give you what you need.

    Hope that helps,
    Priscilla

    -----------------------------------------------------
    Priscilla Walmsley
    Author, Definitive XML Schema (Prentice Hall PTR)
    -----------------------------------------------------


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Priscilla Walmsley, Apr 23, 2004
    #2
    1. Advertising

  3. (Kevin Campbell) writes:

    > ... It
    > seems to me that one of the nice benefits of XML as a structured data
    > format is that element ordering is not necessarily relevant. In my
    > example the order of the elements has no semantic meaning, so I would
    > like authors to be able to not worry about ordering while still
    > gaining the benefits of validated XML.


    If there is no significance attached to the order of
    the elements, then why not specify a fixed order? Otherwise
    you end up with authors trying to understand or create a
    subtle semantic difference between

    <Book>
    <Title />
    <Author />
    <ISBN />
    <Description />
    </Book>

    and

    <Book>
    <Author />
    <Title />
    <ISBN />
    <Description />
    </Book>

    > Does anyone know how to accomplish this using XML Schemas?


    As Priscilla Walmsley has already pointed out, the
    xsd:all construct does exactly what you say you want.

    -C. M. Sperberg-McQueen
    C. M. Sperberg-McQueen, Apr 23, 2004
    #3
  4. (C. M. Sperberg-McQueen) wrote in message news:<>...
    > (Kevin Campbell) writes:
    >
    > As Priscilla Walmsley has already pointed out, the
    > xsd:all construct does exactly what you say you want.
    >
    > -C. M. Sperberg-McQueen


    I thought it would be something simple! Thanks for the help folks. I
    guess I was stuck on that "all" name, and forgot I could still use
    minOccurs="0" on some of them.

    Thanks again,
    Kevin
    Kevin Campbell, Apr 26, 2004
    #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. Markus
    Replies:
    1
    Views:
    1,528
    Markus
    Nov 23, 2005
  2. CJ
    Replies:
    0
    Views:
    747
  3. Naresh Agarwal
    Replies:
    1
    Views:
    1,269
    Martin Honnen
    Mar 7, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,248
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    395
    Joseph Kesselman
    Oct 17, 2007
Loading...

Share This Page