Multiple occurrences of an xml element

P

Pete

Hi,

I want to be able to represent the following xml in a schema

<Data>
<a/>
<b/>
<c subtype='one value'/>
<c subtype='another value'/>
<d/>
etc.
</Data>

Further constraints are (a) in real world, elements may appear in any
order (which appears to constrain me from using sequence), (b) although
several elements are optional, I cannot guarantee that only one element
will be present at any one time (which appears to constrain me from
using choice).

My gut feel is that I should be using <xs:all>, but in an <xs:all>
maxoccurs may not be greatee than one. So, when I throw a document at a
schema, the second occurrence of element c makes the document invalid.

Am I missing something? This seems like an obvious question, but....

I would be happy to approach this differently if anyone has any ideas -
the constraints are as above, and I really do want to run the data file
through a schema before I waste any time processing it.

TIA,
Pete
 
M

Martin Honnen

Pete wrote:

I want to be able to represent the following xml in a schema

<Data>
<a/>
<b/>
<c subtype='one value'/>
<c subtype='another value'/>
<d/>
etc.
</Data>

Further constraints are (a) in real world, elements may appear in any
order (which appears to constrain me from using sequence), (b) although
several elements are optional, I cannot guarantee that only one element
will be present at any one time (which appears to constrain me from
using choice).

My gut feel is that I should be using <xs:all>, but in an <xs:all>
maxoccurs may not be greatee than one. So, when I throw a document at a
schema, the second occurrence of element c makes the document invalid.

Am I missing something? This seems like an obvious question, but....

I think you need to use
<xs:choice minOccurs="0" maxOccurs="unbounded">
as in the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Data" type="DataType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

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

</xs:schema>

That way it is possible that you have zero child elements in <Data> or
several ones and order doesn't matter.

For instance the following instance validates here against that schema
with both MSXML 4 and Xerces-J:

<?xml version="1.0" encoding="UTF-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2004112701Xsd.xml">
<Data>
<a/>
<b/>
<c />
<c />
<d/>
</Data>
<Data>
</Data>
<Data>
<b/>
<c />
<c />
<d/>
<a/>
</Data>
</root>
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top