Defining multiple possibilities for the same element
is it possible in XML Schema to define the same element with several different definitions depending on one attribute.
As Example:
<xsd:element name="Element">
<xsd:complexType>
<xsd:sequence>
<xsd:attribute name="type" fixed="type1"/>
<xsd:seqeuence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Element">
<xsd:complexType>
<xsd:sequence>
<xsd:attribute name="type" fixed="type2"/>
<xsd:attribute name="value" type="xsd:integer"/>
<xsd:seqeuence>
</xsd:complexType>
</xsd:element>
<xsd:element 开发者_如何学Pythonname="RootElement">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Element"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Or is there any other way to solve this problem. It is important that both definitions are named "Element", because I have an application needing that both elements are named the same way. But there is a second application that needs the additional information for type2.
Consider removing the type
attribute and just defining two childnodes with the different structures you wish.
This way you can have
<Element>
<ElementType1>
<!--whatever-->
</ElementType1>
</Element>
and
<Element>
<ElementType2 value="42">
<!--whatever-->
</ElementType1>
</Element>
in the same document.
Try this in the DTD section for Element
:
<!ELEMENT Element (ElementType1?)> <!-- the ? means 1 or 0 -->
<!ELEMENT Element (ElementType2?)>
or rather this if only one is ever allowed under Element
and Element
is not allowed to be empty:
<!ELEMENT Element (ElementType1 | ElementType1)>
You can read more on about DTD element operators.
If you're using XML Schema 1.1, you can use type alternatives. For XML Schema 1.0, look into xsi:type
or an add-on like Schematron.
精彩评论