[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is this premitted ?
- From: Jeni Tennison <mail@jenitennison.com>
- To: Dimiter Naydenov <bluelight@omega.bg>
- Date: Mon, 02 Jul 2001 10:08:13 +0100
Hi Dimiter,
> Is there a way to conditionally set the "required" or "prohibited"
> options for an attribute/child element, based on other element's
> presence/absence ?
As Eric's said, you can't do exactly this in XML Schema. However, if
you're prepared to use the xsi:type attribute, then you can get close.
First define an abstract complex type for your tag element:
<xs:complexType name="tagType" abstract="true" />
Then define two complex types that derive from that type (here by
extension), one with the attribute and one with the element.
<xs:complexType name="tagWithAttr">
<xs:complexContent mixed="true">
<xs:extension base="tagType">
<xs:attribute name="attr" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="tagWithEle">
<xs:complexContent mixed="true">
<xs:extension base="tagType">
<xs:sequence>
<xs:element name="attr" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
[Note that you should change the definition of the attr element in the
above type to be a type other than xs:string if you want to allow it
to take complex values.]
Finally define the tag element to use the generic tagType complex
type:
<xs:element name="tag" type="tagType" />
When the tag element is present in the instance, it can't use the
tagType itself (since that type is abstract), so it has to use one of
the two types that are derived from it. You can declare which one it
uses with the xsi:type attribute:
<tag xsi:type="tagWithAttr" attr="val">text</tag>
<tag xsi:type="tagWithEle">
<attr>complex val</attr>
text
</tag>
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/