Altova Mailing List Archives

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

From: Martin Honnen <mahotrash@-----.-->
Date: 10/3/2008 3:03:00 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?

   <xs:element name="cat">
       <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"/>

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


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.