Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Schema for validating unordered sequences?

Reply
Thread Tools

Schema for validating unordered sequences?

 
 
Kevin Campbell
Guest
Posts: n/a
 
      04-22-2004
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
 
Reply With Quote
 
 
 
 
Priscilla Walmsley
Guest
Posts: n/a
 
      04-23-2004
Kevin,

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

Hope that helps,
Priscilla

-----------------------------------------------------
Priscilla Walmsley http://www.velocityreviews.com/forums/(E-Mail Removed)
Author, Definitive XML Schema (Prentice Hall PTR)
-----------------------------------------------------


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
C. M. Sperberg-McQueen
Guest
Posts: n/a
 
      04-23-2004
(E-Mail Removed) (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
 
Reply With Quote
 
Kevin Campbell
Guest
Posts: n/a
 
      04-26-2004
(E-Mail Removed) (C. M. Sperberg-McQueen) wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question, schema, complex types and unordered multiple elemets davidjones@myself.com XML 3 10-17-2007 05:52 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Validating XML file against an XSD schema in an ASP.NET page simon ames ASP .Net 1 04-11-2004 09:01 AM
XML Schema 0- unordered set containing multiple occuring element Naresh Agarwal XML 1 03-07-2004 12:39 PM
schema unordered element list with any element CJ XML 0 12-17-2003 07:48 PM



Advertisments