XML Schema <choice> constraints

K

Kimmo J?rvikangas

Dear XML (Schema) experts,

As an example I have the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="ROOT_ELEMENT">
<xs:complexType>
<xs:choice>
<xs:element name="FOO1" type="xs:date"/>
<xs:element name="FOO2" type="xs:dateTime"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Which is fine for my schema validator.

However, if I wanted to have the name of the element appearing under
the root-element to ALLWAYS have the same name (for example FOO,
without a number at the end) regardless of the type of the element I
keep getting (MOST OF THE TIME!) this error message:

"Schema error cos-element-consistent - all types of local element
'FOO' must be consistent".

When I look the situation from the point of an instance document I
don't see how the instance document conforming to this schema with
FOO-element (with varying type) could violate W3C XML recommendation.

SOMETIMES my schema validator gives me green light when in "text mode"
and red light in "graphical mode" of this particular situation (in a
bit more complicated schema instance, though) which seems to me a bug
anyway in this schema validator.

What bothers me is that I dunno which of the two is correct; am I
allowed to have in schema under choice-element two elements declared
with the same name, but different type, or is this not allowed (XML
schema itself is a XML document, too, and two elements with same name
but with different type in the same contextual level is not
allowed...)

I am using XMLSPY Enterprise Edition version 2004 rel.2. as the
"schema validator" I was referring to a couple of times.

Appreciate any solid opinions on this,

<kimmo/>
 
M

Martin Honnen

Kimmo J?rvikangas wrote:

As an example I have the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="ROOT_ELEMENT">
<xs:complexType>
<xs:choice>
<xs:element name="FOO1" type="xs:date"/>
<xs:element name="FOO2" type="xs:dateTime"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Which is fine for my schema validator.

However, if I wanted to have the name of the element appearing under
the root-element to ALLWAYS have the same name (for example FOO,
without a number at the end) regardless of the type of the element I
keep getting (MOST OF THE TIME!) this error message:

"Schema error cos-element-consistent - all types of local element
'FOO' must be consistent".

When I look the situation from the point of an instance document I
don't see how the instance document conforming to this schema with
FOO-element (with varying type) could violate W3C XML recommendation.

Well the W3C XML schema specification is not an attempt to reformulate
the already existing XML recommendation, of course the schema
specification imposes restrictions that go beyond the XML recommendation.

In my view the problem with having

<xs:choice>
<xs:element name="FOO" type="xs:date"/>
<xs:element name="FOO" type="xs:dateTime"/>
</xs:choice>

is that then any schema aware parser trying to process an instance can't
decide which type a <FOO> element occuring is supposed to have.

What you could do however is define a union of date and dateTime e.g.

<xs:simpleType name="date-or-dateTime">
<xs:union memberTypes="xs:date xs:dateTime" />
</xs:simpleType>

and then you can have

<xs:complexType>
<xs:sequence>
<xs:element name="date" type="date-or-dateTime"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

and an instance with elements alike

<date>2004-06-18</date>
<date>2004-06-18T13:33:01</date>
 
I

Isabelle

Dear XML (Schema) experts,

As an example I have the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="ROOT_ELEMENT">
<xs:complexType>
<xs:choice>
<xs:element name="FOO1" type="xs:date"/>
<xs:element name="FOO2" type="xs:dateTime"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Which is fine for my schema validator.

However, if I wanted to have the name of the element appearing under
the root-element to ALLWAYS have the same name (for example FOO,
without a number at the end) regardless of the type of the element I
keep getting (MOST OF THE TIME!) this error message:

"Schema error cos-element-consistent - all types of local element
'FOO' must be consistent".

What about this schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="ROOT_ELEMENT">
<xs:complexType>
<xs:choice>
<xs:element name="FOO">
<xs:simpleType>
<xs:union memberTypes="xs:date xs:dateTime"/>
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

it would validate the 2 formats "Date" and "DateTime" under the same
name "FOO".
 
K

Kimmo J?rvikangas

Isabelle, Martin,

Thanks for Your answers.

In this simple case I do agreee that union makes the trick, but I am
afraid I can't use union with complex types (make my day and tell me I
can).

I guess I will simply have to give up the desire (?) to have an
element to appear in instance documents with the same name but various
types - and learn to desire something that fully conforms to W3C
recommendations.

<kimmo/>
 

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

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top