WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (2/6)
[previous] [next] |
Professional XML Schemas
Imagine that we wanted to alter our Customer.xsd
schema so that we could
represent name and address details for customers as below. Note we have also added a containing
element for the name details called Name:
<?xml version = "1.0" ?>
<Customer customerID = "242552">
<Name>
<FirstName>Raymond</FirstName>
<MiddleInitial>G</MiddleInitial>
<LastName>Bayliss</LastName>
</Name>
<Address>
<Street1>10 Elizabeth Place</Street1>
<Town>Paddington</Town>
<City>Sydney</City>
<StateProvinceCounty>NSW</StateProvinceCounty>
<Country>Australia</Country>
<ZipPostCode>2021</ZipPostCode>
</Address>
</Customer>
We also want it to be able to use the same schema to validate details about employees.
In this case the details would be contained in an Employee
element, although the child
elements of each are the same:
<?xml version = "1.0" ?>
<Employee employeeID = "133">
<Name>
<FirstName>Raymond</FirstName>
<MiddleInitial>G</MiddleInitial>
<LastName>Bayliss</LastName>
</Name>
<Address>
<Street1>10 Elizabeth Place</Street1>
<Town>Paddington</Town>
<City>Sydney</City>
<StateProvinceCounty>NSW</StateProvinceCounty>
<Country>Australia</Country>
<ZipPostCode>2021</ZipPostCode>
</Address>
</Employee>
Seeing as both the Customer
and Employee
elements contain
a Name
element and an Address
element, both of which have the same content
models, we can define the Name
and Address
elements globally, and then use
a reference to the global declarations inside the declarations for the
Customer
and Employee
elements. When we want to create a reference to a
globally declared element, we use the ref
attribute on the element declaration,
whose value is the name of the element that we are referencing.
In order to use references to elements we have declared, we will qualify all of the elements defined by XML Schema using a namespace prefix (We will look into the reasons behind this in Chapter 6). This is what the schema looks like now:
<?xml version = "1.0" ?>
<xs:schema xmlns:xs = "https://www.w3.org/2001/XMLSchema">
<xs:element name = "Customer">
<xs:complexType>
<xs:sequence>
<xs:element ref = "Name" />
<xs:element ref = "Address" />
</xs:sequence>
<xs:attribute name = "customerID" type = "integer" />
</xs:complexType>
</xs:element>
<xs:element name = "Employee">
<xs:complexType>
<xs:sequence>
<xs:element ref = "Name" />
<xs:element ref = "Address" />
</xs:sequence>
<xs:attribute name = "employeeID" type = "integer" />
</xs:complexType>
</xs:element>
<xs:element name = "Name">
<xs:complexType>
<xs:sequence>
<xs:element name = "FirstName" type = "string" />
<xs:element name = "MiddleInitial" type = "string" />
<xs:element name = "LastName" type = "string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name = "Address">
<xs:complexType>
<xs:sequence>
<xs:element name = "Street1" type = "string" />
<xs:element name = "Town" type = "string" />
<xs:element name = "City" type = "string" />
<xs:element name = "StateProvinceCounty" type = "string" />
<xs:element name = "Country" type = "string" />
<xs:element name = "ZipPostCode" type = "string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Firstly you will notice the use of the xs:
prefix on all of the elements
defined by XML Schema. This is declared in the root schema
element:
<xs:schema xmlns:xs = "https://www.w3.org/2001/XMLSchema">
Next you can see that both the Customer
element and the Employee
element declarations contain a reference to the Name
and Address
elements:
<xs:element name = "Employee">
<xs:complexType>
<xs:sequence>
<xs:element ref = "Name" />
<xs:element ref = "Address" />
</xs:sequence>
<xs:attribute name = "employeeID" type = "integer" />
</xs:complexType>
</xs:element>
This enables re-use of element declarations and saves repeating the element declarations inside each element. Any other element or complex type definition in the schema could use these globally defined elements. It is helpful whenever we have an element that may appear in more than one place in a document instance.
It is important to note, however, that any globally defined element can be used as the root element of a document. The only way of enforcing only one root element in a document is to only have one globally defined element, and to carefully nest all other element declarations inside complex type definitions. The benefits of this approach are that we can create a structure that can be used to validate fragments of documents without having to define separate schemas for each fragment, and that it allows us to define one schema for several classes of document. So, we would be able to validate the following document against this schema:
<?xml version = "1.0" ?>
<Address>
<Street1>10 Elizabeth Place</Street1>
<Town>Paddington</Town>
<City>Sydney</City>
<StateProvinceCounty>NSW</StateProvinceCounty>
<Country>Australia</Country>
<ZipPostCode>2021</ZipPostCode>
</Address>
This document is considered valid because the Address
element has
been declared globally. This may not be desirable, and we will see other approaches \
as we go through the book. We look into this topic more in Chapter 7.
Note that an element declaration that carries a ref
attribute cannot also carry
a name
attribute, nor can it contain a complex type definition.
[previous] [next] |
Created: October 22, 2001
Revised: October 22, 2001
URL: https://webreference.com/authoring/languages/xml/schemas/chap1/2/2.html