Re: Forcing a parent tag to have at least one child via an XML schema

Discussion in 'XML' started by Mukul Gandhi, Oct 3, 2008.

  1. Mukul Gandhi

    Mukul Gandhi Guest

    On Oct 3, 5:37 pm, "Susan Harris" <> wrote:
    > I have a document that includes info similar to:
    >
    > <cat>
    >   <name>Tiddles</name>
    >   <color>Purple</color>
    >   <age>12</age>
    >   <ageInCatYears>84</ageInCatYears>
    > </cat>
    >
    > ALL elements (including <cat>) are optional. However, <cat> should not exist
    > unless at least one of it children does.
    >
    > How can I specific via an XML schema that <cat> should not exist unless it
    > has at least one child?


    I haven't check the Schema spec deeply, but I feel this might not be
    possible with XSD 1.0. But it should be possible with the upcoming XSD
    1.1 schema language (using the assertions feature).

    With XSD 1.1, this should likely work:

    <xs:element name="cat">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="name" type="xs:string" minOccurs="0"
    maxOccurs="1" />
    <xs:element name="color" type="xs:string" minOccurs="0"
    maxOccurs="1" />
    <xs:element name="age" type="xs:integer" minOccurs="0"
    maxOccurs="1" />
    <xs:element name="ageInCatYears" type="xs:string"
    minOccurs="0" maxOccurs="1" />
    </xs:sequence>
    <xs:assert test="count(*) &gt; 0" />
    </xs:complexType>
    </xs:element>
    Mukul Gandhi, Oct 3, 2008
    #1
    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. Kenneth Love
    Replies:
    2
    Views:
    795
    Kenneth Love
    Apr 1, 2005
  2. Martin Honnen
    Replies:
    0
    Views:
    368
    Martin Honnen
    Oct 3, 2008
  3. srdgame
    Replies:
    3
    Views:
    253
  4. Noel Dolan
    Replies:
    0
    Views:
    217
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page