[xsd-users] polymorphism through xs:choice
Harg Tholan
hargtholan at gmail.com
Tue Jun 11 08:05:11 EDT 2013
Hi,
I have a major doubt regard the use of *idref*.
I have an xs:choice (with maxOccurs=unbounded) of different elements which
types are derived from the same base type, then I have references to such
elements in another point of the schema. The question is, how can I
retrieve the actual type of the referenced element? I need to know the
actual type because I need to "dynamic cast" to it, but I can not know
beforehand. It seems that the use of xs:choice for achieving polymorphism
is an anti-pattern, according to this article here:
http://www.ibm.com/developerworks/xml/library/ws-tip-xsdchoice/index.html
But still, I wonder whether it could be possible to achieve this with XSD
Codesynthesis.
Code snippets:
base type:
<xs:complexType name="Base" abstract="true">
<xs:sequence>
<xs:element name="alive" type="xs:boolean" default="true" />
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
Then the derived types:
<xs:complexType name="DerivedA">
<xs:complexContent>
<xs:extension base="Feature">
<xs:sequence>
<xs:element name="name" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="DerivedB">
[...]
</xs:complexType>
As said, a sequence of choice of derived types:
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="foo" type="DerivedA" />
<xs:element name="bar" type="DerivedB" />
</xs:choice>
</xs:sequence>
Thanks.
More information about the xsd-users
mailing list