Declaring Elements

In W3C XML Schema terminology, elements and attributes are declared. Both elements and attributes can occur in an instance document.

Listing 19.1 shows a simple example of an XML instance document. Listing 19.2 shows a W3C XML Schema document against which Listing 19.1 can be validated.

Listing 19.1. BasicDocument.xml: A Short XML Instance Document
<?xml version="1.0"?> 
<basicDocument> 
Some text content. 
</basicDocument> 

Listing 19.2. BasicDocument.xsd: A Schema for Listing 19.1
					<?xml version='1.0'?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<xsd:element name="basicDocument" type="xsd:string" /> 
</xsd:schema> 

The indicative namespace prefix in the W3C XML Schema document is xsd. The namespace declaration in the start tag of the xsd:schema element associates the xsd namespace prefix with the namespace URI www.w3.org/2001/XMLSchema.

An xsd:element element is used to declare the basicDocument element that is found in the instance document. The content of the basicDocument element is text content only and is declared, using the type attribute of the xsd:element element, to be of type xsd:string. The xsd:string type is one of many built-in types specified in the W3C XML Schema Recommendations.

If you have a slightly more complex instance document, such as the one in Listing 19.3, you must make use of a complex type definition as well as an element declaration.

Listing 19.3. LessBasicDocument.xml: An XML Document with Attributes and Nested Elements
<?xml version='1.0'?> 
<lessBasicDocument> 
 <Person category="celebrity" status="alive"> 
  <Name>Tiger Woods</Name> 
  <Citizenship>United States</Citizenship> 
  <Occupation>Professional Golfer</Occupation> 
 </Person> 
</lessBasicDocument> 

As you can see, the content of the lessBasicDocument element is a hierarchy of nested elements. In addition, the Person element has two attributes, category and status.

Listing 19.4 shows one approach to a schema for Listing 19.3. W3C XML Schema provides flexible tools that allow several approaches to how the structure of an instance document is represented.

Listing 19.4. LessBasicDocument.xsd: A Schema for Listing 19.3
 
<?xml version='1.0'?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

<xsd:element name="lessBasicDocument" type="myPersonType"/> 

<xsd:complexType name="myPersonType"> 
 <xsd:element name="Person" type="PersonType"/> 
</xsd:complexType> 

<xsd:complexType name="PersonType"> 
 <xsd:sequence> 
  <xsd:element name="Name" type="xsd:string" /> 
  <xsd:element name="Citizenship" type="xsd:string" /> 
  <xsd:element name="Occupation" type="OccupationType" /> 
 </xsd:sequence> 
 <xsd:attribute name="category" type="xsd:string" /> 
 <xsd:attribute name="status" type="StatusType" /> 
</xsd:complexType> 

<xsd:simpleType name="OccupationType"> 
 <xsd:restriction base="xsd:string"> 
  <xsd:enumeration value="Professional Golfer" /> 
  <xsd:enumeration value="Actor" /> 
  <xsd:enumeration value="Professional Footballer" /> 
 </xsd:restriction> 
</xsd:simpleType> 

<xsd:simpleType name="StatusType"> 
 <xsd:restriction base="xsd:string"> 
  <xsd:pattern value="alive|dead" /> 
 </xsd:restriction> 
</xsd:simpleType> 

</xsd:schema> 

The document element is declared using this:

<xsd:element name="lessBasicDocument" 
type="myPersonType"/> 

For the allowed content of the lessBasicDocument element, you need to find the definition for the myPersonType complex type:

<xsd:complexType name="myPersonType"> 
 <xsd:element name="Person" type="PersonType"/> 
</xsd:complexType> 

To find the allowed content of the Person element, you need to find the definition of the PersonType complex type:

<xsd:complexType name="PersonType"> 
 <xsd:sequence> 
  <xsd:element name="Name" type="xsd:string" /> 
  <xsd:element name="Citizenship" type="xsd:string" /> 
  <xsd:element name="Occupation" type="OccupationType" /> 
 </xsd:sequence> 
 <xsd:attribute name="category" type="xsd:string" /> 
 <xsd:attribute name="status" type="StatusType" /> 
</xsd:complexType> 

This definition specifies that a Person element is allowed to have a sequence of child elements, as defined by the content of the xsd:sequence element. The Person element may also have two attributes named category and status.

The values of some elements and attributes are simply strings, as indicated by the xsd:string value for the type attribute. The xsd:string type is one of many built-in datatypes in W3C XML Schema.

The allowed content of the Occupation element is defined in the definition for the OccupationType type:

<xsd:simpleType name="OccupationType"> 
 <xsd:restriction base="xsd:string"> 
  <xsd:enumeration value="Professional Golfer" /> 
  <xsd:enumeration value="Actor" /> 
  <xsd:enumeration value="Professional Footballer" /> 
 </xsd:restriction> 
</xsd:simpleType> 

The xsd:restriction element is used to restrict (or constrain) allowed values. The base attribute of xsd:restriction indicates the base type that is being restricted. In this case, the base type is xsd:string. The xsd:enumeration element is used to specify allowed values.

An alternative type of restriction uses the xsd:pattern element. The content of the value attribute of xsd:pattern that defines the allowed values can be a single literal value or a choice of values (as in the following code):

<xsd:simpleType name="StatusType"> 
 <xsd:restriction base="xsd:string"> 
  <xsd:pattern value="alive|dead" /> 
 </xsd:restriction> 
</xsd:simpleType> 

Or, it can be a regular expression.

Declaring Attributes

To declare an attribute, you use the xsd:attribute element. The name attribute of the xsd:attribute element contains the attribute name. The value of an attribute is always a simple type. If the permitted values of the attribute are restricted, the xsd:simpleType element is used with the xsd:restriction child element to constrain the permitted values of the attribute.

Declaration of an attribute inside an xsd:complexType element was shown in Listing 19.4. An alternate approach is to declare the attribute inside an element declaration:

<xsd:element name="Person"> 
 <xsd:complexType > 
  <xsd:sequence> 
   <xsd:element name="Name" type="xsd:string" /> 
   <xsd:element name="Citizenship" type="xsd:string" /> 
   <xsd:element name="Occupation" type="OccupationType" /> 
  </xsd:sequence> 
  <xsd:attribute name="category" type="xsd:string" /> 
  <xsd:attribute name="status" type="StatusType" /> 
 </xsd:complexType> 
<xsd:element> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset