← Prev in month ← Prev in thread

Conditional xml Schema

From
Jagdishwar B <>
To
Date
2006-06-11T09:42:32Z
ID
<>
Thread
Conditional xml Schema
Hi all,

Is it possible to define the xml schema (xsd) conditionally based on
certain values.

For e.g.
My xml document is:
<myRoot>
	<fruit myAttr="Apple">
		<elementApple1>elemAppleOne</elementApple1>
	</fruit>
	<fruit myAttr="Mango">
		<elementMango1>elemMangoOne</elementMango1>
	</fruit>
</myRoot>

the xsd file should define the structure (i mean put the constraints)
in such a way that:
if the <fruit> element has myAttr="Apple", then it should force to
have <elementApple1> child element, and
if the <fruit> element has myAttr="Mango", then it should force to
have <elementMango1> child element

I created following schema,

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="fruit">
		<xs:complexType>
			<xs:choice>
				<xs:element name="elementApple1" type="xs:string"/>
				<xs:element name="elementMango1" type="xs:string"/>
			</xs:choice>
			<xs:attribute name="myAttr" type="xs:string" use="required"></xs:attribute>
		</xs:complexType>
	</xs:element>
	<xs:element name="myRoot">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="fruit" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>


but the above schema is allowing '<fruit myAttr="Apple">' to contain
'<elementMango1>' child element.

basically i would like to have different structure for the <fruit>
elements depending upon the vaule present in myAttr attribute.

Thanks in advance.
Jagdishwar B
← Prev in month ← Prev in thread