W3C Schema for required and optional elements

Discussion in 'XML' started by Philipp, Apr 10, 2008.

  1. Philipp

    Philipp Guest

    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>
     
    Philipp, Apr 10, 2008
    #1
    1. Advertisements

  2. Philipp

    Pavel Lepin Guest

    Philipp <> wrote in
    <>:
    > 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.

    --
    "...a Netscape engineer who shan't be named once passed a
    pointer to JavaScript, stored it as a string and later
    passed it back to C, killing 30..." --Blake Ross
     
    Pavel Lepin, Apr 11, 2008
    #2
    1. Advertisements

  3. Philipp

    Philipp Guest

    Pavel Lepin wrote:
    > Philipp <> wrote in
    > <>:
    >> 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.
    >


    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
     
    Philipp, Apr 11, 2008
    #3
    1. Advertisements

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,924
    Markus
    Nov 23, 2005
  2. Frank
    Replies:
    9
    Views:
    1,945
    David Dorward
    May 3, 2004
  3. Chris Bedford
    Replies:
    0
    Views:
    760
    Chris Bedford
    Aug 21, 2003
  4. MENTAT
    Replies:
    8
    Views:
    1,124
    MENTAT
    Apr 4, 2005
  5. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,566
    Stanimir Stamenkov
    Apr 25, 2005
  6. Steve Jorgensen
    Replies:
    0
    Views:
    683
    Steve Jorgensen
    Aug 9, 2005
  7. Replies:
    1
    Views:
    495
  8. Spaulding

    Required and optional elements

    Spaulding, Aug 14, 2009, in forum: XML
    Replies:
    1
    Views:
    1,048
    Martin Honnen
    Aug 15, 2009
Loading...