June 2008
Intermediate to advanced
986 pages
27h 8m
English
A substitution group is a set of elements that can be
substituted for each other. We’ll add an <address> element to our schema, then
we’ll define two types of address that can be used instead. Here’s the
schema:
<?xml version="1.0" encoding="UTF-8"?> <!-- substitute.xsd --> <xs:schema xmlns="http://www.oreilly.com/xslt" targetNamespace="http://www.oreilly.com/xslt" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="birthday"/> <xs:element ref="age"/> <xs:element ref="address"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="name" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> <xs:element name="age" type="xs:positiveInteger"/> <xs:element name="address" type="xs:string"/> <xs:element name="businessAddress" type="xs:string" substitutionGroup="address"/> <xs:element name="residentialAddress" type="xs:string" substitutionGroup="address"/> </xs:schema>
The schema defines two additional elements, <businessAddress> and <residentialAddress>, that can be
substituted for <address>.
Notice that the two additional elements use the substitutionGroup attribute to reference the
element they can replace. Also, all of the elements are the same type.
Here’s a valid XML document:
<?xml version="1.0" encoding="utf-8"?>
<!-- substitute.xml --> <person xmlns="http://www.oreilly.com/xslt" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oreilly.com/xslt ...Read now
Unlock full access