Schema for validating unordered sequences?

K

Kevin Campbell

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
 
P

Priscilla Walmsley

Kevin,

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

Hope that helps,
Priscilla
 
C

C. M. Sperberg-McQueen

... 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 />
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
 
K

Kevin Campbell

(e-mail address 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top