Chapter 2: A quick tour of XML Schema
Book Examples
Example 2-1. Product instance
<product effDate="2001-04-02">
<number>557</number>
<size>10</size>
</product>
Example 2-2. Product schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="product" type="ProductType"/>
<xsd:complexType name="ProductType">
<xsd:sequence>
<xsd:element name="number" type="xsd:integer"/>
<xsd:element name="size" type="SizeType"/>
</xsd:sequence>
<xsd:attribute name="effDate" type="xsd:date"/>
</xsd:complexType>
<xsd:simpleType name="SizeType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="2"/>
<xsd:maxInclusive value="18"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Example 2-3. Elements with simple types
<size>10</size>
<comment>Runs large.</comment>
<availableSizes>10 large 2</availableSizes>
Example 2-4. Elements with complex types
<size system="US-DRESS">10</size>
<comment>Runs <b>large</b>.</comment>
<availableSizes><size>10</size><size>2</size></availableSizes>
Example 2-5. Attributes with simple types
system="US-DRESS"
availableSizes="10 large 2"
Example 2-6. Anonymous type
<xsd:element name="size">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="2"/>
<xsd:maxInclusive value="18"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Example 2-7. Elements with complex types
<size system="US-DRESS">10</size>
<product>
<number>557</number>
<size>10</size>
</product>
<letter>Dear <custName>Priscilla Walmsley</custName>...</letter>
<color value="blue"/>
Example 2-8. More complicated content model
<xsd:complexType name="ProductType">
<xsd:sequence>
<xsd:element name="number" type="xsd:integer"/>
<xsd:choice minOccurs="0" maxOccurs="3">
<xsd:element name="size" type="SizeType"/>
<xsd:element name="color" type="ColorType"/>
</xsd:choice>
<xsd:any namespace="##other"/>
</xsd:sequence>
<xsd:attribute name="effDate" type="xsd:date"/>
</xsd:complexType>
Example 2-9. Complex type extension
<xsd:complexType name="ShirtType">
<xsd:complexContent>
<xsd:extension base="ProductType">
<xsd:sequence>
<xsd:element name="color" type="ColorType"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Example 2-10. Product schema document with target namespace
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/prod"
xmlns:prod="http://example.org/prod">
<xsd:element name="product" type="prod:ProductType"/>
<xsd:complexType name="ProductType">
<xsd:sequence>
<xsd:element name="number" type="xsd:integer"/>
<xsd:element name="size" type="prod:SizeType"/>
</xsd:sequence>
<xsd:attribute name="effDate" type="xsd:date"/>
</xsd:complexType>
<xsd:simpleType name="SizeType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="2"/>
<xsd:maxInclusive value="18"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Example 2-11. Instance with namespace
<prod:product xmlns:prod="http://example.org/prod"
effDate="2001-04-02">
<number>557</number>
<size>10</size>
</prod:product>
Example 2-12. Schema composition using include and import
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/ord"
targetNamespace="http://example.org/ord">
<xsd:include schemaLocation="moreOrderInfo.xsd"/>
<xsd:import namespace="http://example.org/prod"
schemaLocation="productInfo.xsd"/>
<!--...-->
</xsd:schema>
Example 2-13. Using xsi:schemaLocation
<prod:product xmlns:prod="http://example.org/prod"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.org/prod prod.xsd"
effDate="2001-04-02">
<number>557</number>
<size>10</size>
</prod:product>
Example 2-14. Annotation
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:doc="http://example.org/doc">
<xsd:element name="product" type="ProductType">
<xsd:annotation>
<xsd:documentation xml:lang="en"
source="http://example.org/prod.html#product">
<doc:description>These elements represent a product.
</doc:description>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:schema>
|