XML Schema <choice> constraints

Discussion in 'XML' started by Kimmo J?rvikangas, Jun 18, 2004.

  1. 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/>
     
    Kimmo J?rvikangas, Jun 18, 2004
    #1
    1. Advertising

  2. 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>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 18, 2004
    #2
    1. Advertising

  3. Kimmo J?rvikangas

    Isabelle Guest

    (Kimmo J?rvikangas) wrote in message news:<>...
    > 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".
     
    Isabelle, Jun 18, 2004
    #3
  4. 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/>
     
    Kimmo J?rvikangas, Jun 18, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,541
    Markus
    Nov 23, 2005
  2. Olaf Meyer
    Replies:
    2
    Views:
    691
    Olaf Meyer
    Jan 15, 2004
  3. Olaf Meyer

    XML schema uniqueness constraints

    Olaf Meyer, Jan 15, 2004, in forum: XML
    Replies:
    0
    Views:
    506
    Olaf Meyer
    Jan 15, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,264
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    2,870
Loading...

Share This Page