Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD: force an XML element to have >=1 subelements

Status
Not open for further replies.

persim95

Programmer
Jan 7, 2006
2
CH
Hi all,

I have a question to XSD and hope that someone here can help me (it's quite urgent...)

I have an XML element 'parent' which can have subelements of three different types: 'child_alpha', 'child_beta' and 'child_gamma'. The occurrence of the single subelements is not restricted: every subelement may appear an unbounded number of times or may not appear at all. But at the same time I want to force the element 'parent' to have at least one subelement. Is this possible?

Example:
Code:
<parent name='p'>
  <child_alpha>1000</child_alpha>
  <child_beta>100</child_beta>
  <child_alpha>100</child_alpha>  
  <child_alpha>10000</child_alpha>  
</parent>
is ok,
Code:
<parent name='p'>
  <child_gamma>1000000</child_alpha>
</parent>
is ok, while
Code:
<parent name='p'>
</parent>
is not permitted.

I hope my explanation of the problem is clear: I don't want to force a certain subelement, say 'child_alpha', to appear at least once, because 'child_alpha' may also not appear. But I want that at least one of the three possible subelements appears inside the 'parent' element.

I have checked some XML Schema tutorials, but I have only found how to restrict the occurrence of a certain element - not how to restrict the occurrence of subelements.

Does somebody know a workaround to my problem?

Thank you very much in advance for your answers!!

Simon


 

Thank you for ignoring my thread.

In the meantime I have found a solution to the requirement. Maybe eventually someone may also need it...

<xs:element name="parent">
<xs:complexType>
<xs:sequence>
<xs:group ref="g1"/>
<xs:group ref="g1" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:group name="g1">
<xs:choice>
<xs:element name="child_alpha" type="xs:string"/>
<xs:element name="child_beta" type="xs:string"/>
<xs:element name="child_gamma" type="xs:string"/>
</xs:choice>
</xs:group>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top