Reputation: 81
I have this xml :
<hierarchy>
<unix launcher="LauncherUnix">
<linux/>
<aix/>
</unix>
<bdd>
<oracle launcher="LauncherOracle"/>
<mysql launcher="LauncherMySQL"/>
</bdd>
</hierarchy>
This is a simple tree and I want to validate it with an xsd. The main scheme is a root element name hierarchy
and after I just have several tag with name which can have the attribute launcher
.
I try to set a type to an xs:any
like this :
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="hierarchy" type="nodeHierarchy"/>
<xs:complexType name="nodeHierarchy">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:any type="nodeHierarchy"/>
</xs:sequence>
<xs:attribute name="launcher" use="optional"/>
</xs:complexType>
</xs:schema>
But I get an error because a xs:any
can't have a type. After this I try to change the xs:any
with xs:element
like this :
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="hierarchy" type="nodeHierarchy"/>
<xs:complexType name="nodeHierarchy">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element type="nodeHierarchy"/>
</xs:sequence>
<xs:attribute name="launcher" use="optional"/>
</xs:complexType>
</xs:schema>
The problem is taht an xs:element
must have a name.
Is it possible to have an simple xsd solve this problem ?
Upvotes: 1
Views: 816
Reputation: 43671
Is it possible to have an simple xsd solve this problem ?
Simple? No. But it's possible with substitution groups.
What you basically want is to have elements of the certain types (and possibly subtypes) but different names.
Define an abstract element of the desired type:
<xsd:element name="_node" type="nodeHierarchy" abstract="true"/>
You can reference this element in your complex type:
<xsd:complexType name="nodeHierarchy">
<xsd:sequence>
<xsd:element ref="_node" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="launcher" use="optional"/>
</xsd:complexType>
Then define elements which can substitute _node
:
<xsd:element name="oracle" type="nodeHierarchy" substitutionGroup="_node"/>
<xsd:element name="mysql" type="nodeHierarchy" substitutionGroup="_node"/>
Now these elements may substitute _node
everywhere it is used.
From my point of view this is not the best schema design. Do not implement business logic in XML element names.
Upvotes: 1