OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is this premitted ?

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:complexType name="tagWithEle">
   <xs:complexContent mixed="true">
      <xs:extension base="tagType">
            <xs:element name="attr" type="xs:string" />

[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

<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>



Jeni Tennison