Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old October 3rd, 2008, 01:45 PM
Susan Harris
Guest
 
Posts: n/a
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, <catshould not exist
unless at least one of it children does.

How can I specific via an XML schema that <catshould not exist unless it
has at least one child?

  #2  
Old October 3rd, 2008, 01:55 PM
Mukul Gandhi
Guest
 
Posts: n/a

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


On Oct 3, 5:37*pm, "Susan Harris" <susanbharris1...@yahoo.comwrote:
Quote:
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, <catshould not exist
unless at least one of it children does.
>
How can I specific via an XML schema that <catshould 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>

  #3  
Old October 3rd, 2008, 02:05 PM
Martin Honnen
Guest
 
Posts: n/a

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


Susan Harris wrote:
Quote:
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, <catshould not
exist unless at least one of it children does.
>
How can I specific via an XML schema that <catshould not exist unless
it has at least one child?

<xs:element name="cat">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="name" type="xs:string"/>
<xs:element name="color" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
<xs:element name="ageInCatYears" type="xs:int"/>
</xs:choice>
</xs:complexType>
</xs:element>

would suffice to ensure that the 'cat' element has at least one of the
child elements listed in the choice. On the other hand the above
definition would allow several 'name' or 'color' or 'age' elements so I
doubt you want that construct.

So whether there is a solution depends on the other constraints you want
to put on the children of 'cat'.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Closed Thread