Is it possible to define an attribute of an XML element to be required only if another element's attribute is set to a particular value?



I would like to ask if this is possible and if yes, how to achieve it:


For example if I have an element definition with a complexType in an XML schema definition like this:



<xsd:element name="tag" type="tag"/>
<xs:complexType name="tag">
<xs:choice maxOccurs="unbounded">
<xs:element name="subtag" type="subtag"/>
<xs:element name="another_subtag" type="another_subtag"/>
<xs:element name="another_subtag_2" type="another_subtag_2"/>
</xs:choice>
<xs:attribute name="type" type="attr_type"/>
<xs:attribute name="an_attr" type="an_attr"/>
<xs:attribute name="another_attr" type="another_attr"/>
</xs:complexType name="attr_type">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="type_1"/>
<xsd:enumeration value="type_2"/>
<xsd:enumeration value="type_3"/>
</xsd:restriction>
<xsd:simpleType/>


1) Is it possible to make the attribute 'an_attr' of the tag element required only if the tag element has the attribute 'attr_type' set to 'type_2'?


And another question: 2) Is it possible to make the complexType 'tag' contain different child elements based again e.g. on the value of the 'attr_type'? For example for:



<tag attr_type="type_1">


have only this childs:



<xs:element name="subtag" type="subtag"/>


And for:



<tag attr_type="type_2">


have only childs:



<xs:element name="another_subtag" type="another_subtag"/>
OR
<xs:element name="another_subtag_2" type="another_subtag_2"/>


?


If it is possible, how can I achieve this?


Thanks for the attention!


No comments:

Post a Comment