Substitution group hierarchies
Just as Gosu reproduces XSD-defined type hierarchies in its type system, Gosu also exposes
XSD-defined substitution group hierarchies (substitution groups). You can
define an XSD substitutionGroup attribute on any top-level element to
indicate the QName of another top-level element for which it can
substitute..
To create a substitution group member, set the XML attribute substitutionGroup
on an element to the element name (QName) of the substitution group
head. The typical usage is to create a substitution group head (the group main element)
with an abstract name (for example, "Address").
If an XML element uses a substitution
group member QName in
place of the head’s QName,
the Gosu XML processor knows that the substitution happened. However,
to cast the type to the expected type for the substitution, call the
cast method on the result
with no arguments. For example:
xml.Address = new schema.UKAddress().cast()Example
Create myschema.xsd in the package examples.pl.gosu.xml:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="Customer">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Address"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Address"/>
<xsd:element name="USAddress" substitutionGroup="Address"/>
<xsd:element name="UKAddress" substitutionGroup="Address"/>
</xsd:schema>
The following code creates an Address property and uses the
substitution group hierarchy to cast that property to a UKAddress:
uses examples.pl.gosu.xml.myschema.Customer
uses examples.pl.gosu.xml.myschema.UKAddress
var xml = new Customer()
xml.Address = new UKAddress().cast()
xml.print()
This code prints the following:
<?xml version="1.0"?>
<Customer>
<UKAddress/>
</Customer>
The XML Schema specification requires that the XSD type of a substitution group member be a
subtype of the XSD type of its substitution group head. In the example above,
UKAddress, USAddress and
Address are all of the type xsd:anyType, which
is the default when there is no explicit type.
