By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

XML Schema - Element Order

P: n/a
Hi,

I have read various articles on this and I have had a few suggestions
but would like to know which is correct.

I have an XML file with the element <root>

Within <root> I can have a number of child elements occuring in any
order and any number of times.

eg ....

XML File

<root>
<a/>
<a/>
<c/>
<a/>
<b/>
</root>

I have been reading it is the way I declare the <xs:choice> element so
have thought of this schema below. This should allow me to use element
a, b, or c in any order and as many times as possible.

Schema File

<xs:element name="root">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
<xs:element ref="c"/>
</xs:choice>
</xs:complexType>
</xs:element>

Does this snippet of my schema do what I want correctly or does it do
something different?

Any advice on this would be greatly appreciated

Jonathan Tremlett

Mar 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Yes, it does exactly what you described.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Mar 15 '06 #2

P: n/a
I have a similar situation as Jonathan, but with one more requirement.
I would like to be able to restrict how many times some of the elements
appear.

I was hoping I could write a schema like the following where element
'a' is restricted to maxOccur of 3:

<xs:element name="root">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="a" maxOccurs="3"/>
<xs:element name="b"/>
<xs:element name="c"/>
</xs:choice>
</xs:complexType>
</xs:element>

This element level restriction doesn't seem to work... at least with
..Net's schema validation.

Any ideas?

Thanks,
Jammy

Mar 15 '06 #3

P: n/a
The simplest thing in this case is to use Schematron embedded rules to
check the occurences of a. A working schema that does this is below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="Check
number of occurances">
<rule context="root">
<assert test="count(a)&lt;=3">There should maximum 3
occurences of a in root.</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="a"/>
<xs:element name="b"/>
<xs:element name="c"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

For your example one can write a complex content model to hanlde what
you want like below:

(b|c)*,(a,(b|c)*, (a,(b|c)*, (a, (b|c)*)?)?)?

that in XML Schema notation can be written as

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:group ref="others" minOccurs="0" maxOccurs="unbounded"/>
<xs:sequence minOccurs="0">
<xs:element name="a"/>
<xs:group ref="others" minOccurs="0" maxOccurs="unbounded"/>
<xs:sequence minOccurs="0">
<xs:element name="a"/>
<xs:group ref="others" minOccurs="0"
maxOccurs="unbounded"/>
<xs:sequence minOccurs="0">
<xs:element name="a"/>
<xs:group ref="others" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:sequence>
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:group name="others">
<xs:choice>
<xs:element name="b"/>
<xs:element name="c"/>
</xs:choice>
</xs:group>
</xs:schema>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Mar 15 '06 #4

P: n/a
After I thought about this I understand why this doesn't work. The
minOccurs/maxOccurs on xs:choice is how many choices are allowed. So
'a' can be selected multiple times and each time with a maxOccurs of 3.

Still, if anyone has a suggestion how I can have an unordered list
where every item can appear 0/1 times and some items more.... please
let me know.

Thanks,
Jammy

Mar 15 '06 #5

P: n/a
Remember, Schema is *NOT* intended to cover every possible constraint on
the data, just a useful common set of them. It is still, and always,
your application code's responsibility to impose any checks that Schema
can't handle.

Think of schema more as a first stage than as a complete solution.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.