Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Schema for validating unordered sequences? (http://www.velocityreviews.com/forums/t166973-schema-for-validating-unordered-sequences.html)

Kevin Campbell 04-22-2004 08:20 PM

Schema for validating unordered sequences?
 
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

Priscilla Walmsley 04-23-2004 12:07 AM

Re: Schema for validating unordered sequences?
 
Kevin,

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

Hope that helps,
Priscilla

-----------------------------------------------------
Priscilla Walmsley priscilla@walmsley.com
Author, Definitive XML Schema (Prentice Hall PTR)
-----------------------------------------------------


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

C. M. Sperberg-McQueen 04-23-2004 05:49 PM

Re: Schema for validating unordered sequences?
 
kcsasquatch@yahoo.com (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

Kevin Campbell 04-26-2004 04:59 PM

Re: Schema for validating unordered sequences?
 
cmsmcq@acm.org (C. M. Sperberg-McQueen) wrote in message news:<m2llkm1t5m.fsf@acm.org>...
> kcsasquatch@yahoo.com (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


All times are GMT. The time now is 10:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.