W3C Schema for required and optional elements

P

Philipp

Hello,
I'm learning W3C Schema 1.0 and have encountered a problem. I want to
make a complex type which requires to contain exactly 1 instance of each
of a list of elements (a, b, c) and at least once a choice of several
other elements ( d1 | d2 | d3 ). This in any order.

I have come up with the following which is (unfortunately) invalid XSD 1.0:
<complexType name="root">
<all>
<element name="a" type="string"></element>
<element name="b" type="string"></element>
<element name="c" type="string"></element>
<choice maxOccurs="unbounded" minOccurs="1">
<element name="d1" type="string"></element>
<element name="d2" type="string"></element>
<element name="d3" type="string"></element>
</choice>
</all>
</complexType>

If somebody can help me make up a valid schema for this, I would be very
thankful. Below are some XML examples to clarify what I expected.

Thanks Phil

=== XML example ===

VALID (a,b,c are there, and at least one d*)
<root>
<a />
<b />
<c />
<d1 />
</root>

VALID (order changed, that's OK)
<root>
<a />
<d1 />
<c />
<b />
</root>

VALID (multiple d* are OK)
<root>
<a />
<d2 />
<b />
<c />
<d3 />
<d2 />
</root>

INVALID (a appears twice, bad)
<root>
<a />
<b />
<c />
<d1 />
<a />
</root>

INVALID (no d* element, bad)
<root>
<a />
<b />
<c />
</root>
 
P

Pavel Lepin

Philipp said:
I'm learning W3C Schema 1.0 and have encountered a
problem. I want to make a complex type which requires to
contain exactly 1 instance of each of a list of elements
(a, b, c) and at least once a choice of several other
elements ( d1 | d2 | d3 ). This in any order.

I have come up with the following which is (unfortunately)
invalid XSD 1.0: <complexType name="root">
<all>
<element name="a" type="string"></element>
<element name="b" type="string"></element>
<element name="c" type="string"></element>
<choice maxOccurs="unbounded" minOccurs="1">
<element name="d1" type="string"></element>
<element name="d2" type="string"></element>
<element name="d3" type="string"></element>
</choice>
</all>
</complexType>

W3C's schema definition language was not designed for
defining arbitrarily complex grammars. If your document
format is well-structured, expressing it in XML Schema
should be easy. If it's not, try using a more powerful
schema definition language or check validity on the
application side.
 
P

Philipp

Pavel said:
W3C's schema definition language was not designed for
defining arbitrarily complex grammars. If your document
format is well-structured, expressing it in XML Schema
should be easy. If it's not, try using a more powerful
schema definition language or check validity on the
application side.

After some reading on the web, I think my biggest problem is that my
elements are non-ordered. This makes sense for me both from the model
perspective (I don't care if eg. my Book object sets the Author or the
Title first) as well as from my implementation perspective (these values
are marshalled from a java Properties object which does not guaratee a
constant traversal order for its iterator.

Hopefully Xerces for Schema 1.1 will soon be available :) (and also
that Schema 1.1 fixes these shortcommings)

Phil
 

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,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top