Using XML Schema to Force At Least One Item?

Discussion in 'XML' started by Kenneth Love, Apr 1, 2005.

  1. Kenneth Love

    Kenneth Love Guest

    Sorry if this is a newbie question. I couldn't get the correct magic
    incantation for Google to get an answer. :-(

    I'm trying to satisfy a requirement that "at least one of the following
    *must* be present" in an XML Schema. I have come up with two examples
    that almost work.

    Here's the first:

    <xs:element name="LightExample1" maxOccurs="3">
    <xs:complexType>
    <xs:choice>
    <xs:element name="A"/>
    <xs:element name="B"/>
    <xs:element name="C"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>

    This doesn't work because it allows multiple A, B, or C elements.

    Here's the second example:

    <xs:element name="LightExample2">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="A" minOccurs="0"/>
    <xs:element name="B" minOccurs="0"/>
    <xs:element name="C" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    This *almost* works. The only problem is that it allows an empty
    <LightExample2/> element which fails the "at least one of" portion of
    the requirement.

    Is it possible to do this?

    Kenneth
     
    Kenneth Love, Apr 1, 2005
    #1
    1. Advertising

  2. Kenneth Love wrote:

    > I'm trying to satisfy a requirement that "at least one of the following
    > *must* be present" in an XML Schema. I have come up with two examples
    > that almost work.
    >
    > Here's the first:
    >
    > <xs:element name="LightExample1" maxOccurs="3">
    > <xs:complexType>
    > <xs:choice>
    > <xs:element name="A"/>
    > <xs:element name="B"/>
    > <xs:element name="C"/>
    > </xs:choice>
    > </xs:complexType>
    > </xs:element>
    >
    > This doesn't work because it allows multiple A, B, or C elements.


    Why do multiple elements conflict with your sole requirement "at least
    one of the following *must* be present"?

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 1, 2005
    #2
    1. Advertising

  3. Kenneth Love

    Kenneth Love Guest

    Okay. I guess I left out the requirement that no more than one of each
    element A, B, or C can appear in the result.

    ABC is valid. C is valid. AA is not. CBC is not.

    The order is not that important to me. If I was provided a solution
    forcing the elements to be in a certain order then that's okay. It's
    okay if the solution provides for the elements to be in any order. I
    would prefer the former, actually.

    I could write a regular expression like this:

    RE = (A, B?, C?) | (A?, B, C?) | (A?, B?, C)

    Any thoughts?

    adTHANKSvance,
    Kenneth Love
     
    Kenneth Love, Apr 1, 2005
    #3
    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,677
    Markus
    Nov 23, 2005
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,335
    Stanimir Stamenkov
    Apr 25, 2005
  3. Mukul Gandhi
    Replies:
    0
    Views:
    520
    Mukul Gandhi
    Oct 3, 2008
  4. Martin Honnen
    Replies:
    0
    Views:
    386
    Martin Honnen
    Oct 3, 2008
  5. AAaron123
    Replies:
    0
    Views:
    651
    AAaron123
    Oct 3, 2008
Loading...

Share This Page