There are two different styles of substitution in XML Schema. It is my
experience that they are frequently confused. Based on all the replies
in this thread, that is the case here.
The use of substitution groups in a schema is detectable
by the use of the attribute @substitutionGroup on one
or more global elements. Note that none of the
replies in this thread have "substitutionGroup" attributes,
I will add them in the appropriate place below.
JAXB supports substitution groups completely.
Think of substitution groups as a dynamic choice that
can be exended by other schemas. Any global element declaration that
has or extends the base type of the header element of
a substitution group, can add itself to the substitution group
of the header element. All elements in element substitution group
have the same xml datatype or derive from that xml datatype.
Type substitution manifests itself as an attribute
"xsi:type" in the XML instance document. JAXB Standard Implementation
1.0.1 does not support type substitution and that is all that
can be accomplished with the example as it is below.
See how the example below can be made to use element substitution groups
by seeing the additions inlined below. At end there are instance documents
illustrating XML Schema element substitution and type substitution.
Ganga Sah wrote:
>How about this:
>
><xs:schema ..>
>
Assume targetNamespace and default namespace are same for this
example. Otherwise all global element references would need
a namespace prefix. (Just trying to keep things simple.)
><xs:ComplexType name="Animal">
> <xs:attribute name="name" type="xs:string"
>use="required"/>
></xs:ComplexType>
>
><xs:ComplexType name="dogType">
> <xs:ComplexContent>
> <xs:extension base="Animal/>
> <!--add dog specific elements/attr-->
> </xs:ComplexContent>
></xs:ComplexType>
>
><xs:ComplexType name="catType">
> <xs:ComplexContent>
> <xs:extension base="Animal/>
> <!--add cat specific elements/attr-->
> </xs:ComplexContent>
></xs:ComplexType>
>
<!-- Declare substitution group. Substitution group attribute only
supported on global element declarations. Only global
element declaration can be head element of substitution group. -->
<xs:element name="animal" type="Animal"/>
<xs:element name="dog" type="dogType" substitutionGroup="animal"/>
<xs:element name="cat" type="catType" substitutionGroup="animal"/>
><xs:element name="Root">
>
<xs:complexType>
><xs:sequence>
>
<!-- Element "animal" is the head element. Either an animal, cat
or dog element
can occur in the instance document. Think of animal as a
choice over
all elements in "animal" 's subsitution group. -->
> <xs:element ref="animal">
></xs:sequence>
></xs:complexType>
>
</xs:element>
></xs:schema>
>
>
>
Following 3 instance documents use element substitution:
(Note: I did not concentrate on getting namespace exactly correct.)
1. Use header
<root xmlns="XXXX">
<animal name="Foo"> ...</animal>
</root>
2. Substitute "cat" element for "animal" element. (in same element
substitution group)
<root>
<cat name="Foo"> ... </cat>
</root>
3. Substitute "dog" element for animal element.
<root>
<dog name="Lassie"> ....</dog>
</root>
********************
Type substitution would be accomplished in the following manner with above
schema. Note that with type substitution, the content of the element is
dictated by the "xsi:type" attribute.
<root>
<animal xsi:type="cat"> ...cat content model </animal>
</root>
I hope these examples clear up the confusion.
More complete treatments of "element substitution group" and
"type substitution" are at the following URLs. I have oversimplified
a bit in the discussions above.
Element substitution group is explained in the XML Schema
Primer at the following URL:
http://www.w3.org/TR/xmlschema-0/#SubsGroups.
Type substitution is explained in detail in
http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs.
-Joe Fialli, Sun Microsystems
>--- Gary Gregory <ggregory_at_seagullsw.com> wrote:
>
>
>>IOW, how do I do this:
>>
>>Snoopy.xml:
>><dog name="Snoopy"><!-- dog elements --></dog>
>>
>>Felix.xml:
>><cat name="Felix"><!-- cat elements --></cat>
>>
>>And get
>>
>>interface Dog extends Animal
>>interface Cat extends Animal
>>Animal implements get/setName()
>>
>>?
>>
>>Thanks,
>>Gary
>>
>>
>>-----Original Message-----
>>From: Gary Gregory
>>Sent: Thursday, June 05, 2003 16:13
>>To: 'Discussion list for the Java Architecture for
>>XML Binding'
>>Subject: Still no Substitution group support?
>>
>>Hello,
>>
>>>From the 1.0 spec:
>>
>>"Substitution group
>>Any XML Schema concepts indicating that substitution
>>group support is
>>a necessary component of the schema should be
>>reported as an error."
>>
>>When will Substitution group be supported?
>>
>>Is my only alternative is to use a vendor extension
>>to specify a common
>>supertype?
>>
>>Thanks,
>>Gary
>>
>>
>>
>
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
>http://calendar.yahoo.com
>
>