Introduction To XML Schema
Introduction To XML Schema
· Child elements, the order of child elements, and the number of child elements
· Data types
· Extensibility
· XML syntax
· Constraints
XML Schema: Data Types
XML schema supports 44 built-in data types such as string, decimal, and integer. These
data types help identify the type of data that an element or attribute can contain. For
example, an integer data type can restrict an EmpNumber element to contain only
numeric data. In contrast, DTDs use PCDATA and CDATA that do not identify the type of
data that an element or attribute can contain. For example, PCDATA does not describe
whether data is character or number type
XML Schema: Extensibility
XML schemas are extensible because these schemas allow you to:
· The EmpDetails element must contain only one EmpDept element, followed by
the EmpName, EmpAddress, and EmpAge elements.
· The EmpNumber element must contain exactly eight characters.
· The EmpInitials element contains data that follows the regular expression [A-Z]
[A-Z].
XML Schema Namespaces
The XML schema namespace identifies the keywords that define the structure of an
XML schema document. Every XML schema namespace is associated with the
following namespaceURI:
http://www.w3.org/2001/XMLSchema
The graphic on the right displays the commonly used keywords contained in the XML
schema namespace. These keywords help define the following sections of the XML
schema document:
· Schema definition
· Element declarations
· Attribute declarations
Schema Definition
Unlike a DTD document, an XML schema document cannot be declared within an XML
document. An XML schema document is defined in a separate document that has a .xsd
extension.
Every XML schema document contains a schema element as the root element. The
schema element may contain attributes such as the namespace declaration. For
example, in the code snippet on the right, the schema element is defined as:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Note the following:
· xs:schema indicates the schema element with the xs prefix. Note that a prefix
can take any value, which is typically unique within an XML schema document.
· data_type indicates the data type specified for the element. XML schema
provides built-in data types, such as xs:string, xs:date, xs:time, xs:decimal, and
xs:boolean.
· value indicates the default or fixed value for the element. The default/fixed
keyword is optional.
For example, in the following code snippet, firstname is the name of an element and
xs:string indicates that the string data type is specified for the firstname element.
Therefore, the firstname element can contain only character strings.
<xs:element name="firstname" type="xs:string" />
Similarly, the lastname element can be declared as:
<xs:element name="lastname" type="xs:string" />
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmpName">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname"
type="xs:string" />
<xs:element name="lastname" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
// Additional declarations...
Element Declarations: Simple Type (continued)
A simple type element can also be declared as:
<xs:element name="Element_Name">
<xs:simpleType>
...
...
</xs:simpleType>
where Element_Name indicates the name of an element. Note that
this declaration does not contain the type keyword, which indicates
the data type specified for the element. This declaration is commonly
used to specify restrictions for the element data types.
Restrictions
Restrictions are used to define permissible values for elements and attributes.
Restrictions enable modification of data types to create custom data types. Restrictions
on XML elements are called facets. Facets are used within the restriction element.
The commonly used facets are:
· enumeration
· pattern
· length
Facets: minInclusive and maxInclusive
The minInclusive and maxInclusive facets are used to define the minimum and
maximum numeric value that an element can contain. For example, in the following
code snippet, the minInclusive and maxInclusive facets indicate that the EmpAge
element can contain only integer values between 0 and 100.
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmpAge">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
// Additional declarations...
Facets: enumeration
The enumeration facet can be used to define a constraint for an element to contain only
specified values. These values depend upon the data types mentioned in the restriction
element. For example, in the code snippet on the right, the enumeration facet indicates
that the EmpID element can take only three string values: ABC, LMN, and XYZ.
Similarly, in the following code snippet, the enumeration facet indicates that the priority
element can take only three numeric values: 1, 2, and 3.
<xs:element name="priority">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DeptID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ABC"/>
<xs:enumeration value="LMN"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
// Additional declarations...
Facets: pattern
The pattern facet can be used to constraint an element to contain only a series of
characters. A commonly used pattern value is a regular expression. For example, in the
code snippet on the right, the EmpInitials element can contain only two uppercase
letters between A and Z.
Similarly, if the pattern value is as follows, [Abc] indicates that the element can contain
only three permissible values: A, b, or c.
<xs:pattern value="[Abc]"/>
If pattern value="[a-zA-Z][[a-zA-Z]", the element can contain only two lowercase or
uppercase characters between the letters A and Z.
In addition, if pattern value="[0-9][[0-9]", the element can contain only two numeric digits
between 0 and 9.
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmpInitials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
// Additional declarations...
Facets: length
The length facet can be used to constrain an element to contain characters of a fixed
length. For example, in the code snippet on the right, 8 indicates that the EmpNumber
element can contain only eight characters.
Similarly, in the following code snippet, 6 indicates that the password element can
contain only six integers.
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:length value="6"/>
</xs:restriction>
You can reuse element definitions using the name attribute in simpleType elements. The
name attribute enables you to specify a name that refers to the simpleType element
definitions. For example, the following code snippet uses the name attribute to name the
simpleType element definition as typename:
<xs:simpleType name="typename">
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:simpleType>
To reuse the above definition, you need to specify typename as a value for the type
attribute in simple type element declarations. For example, in the following declaration,
the EmpNumber element reuses the preceding simpleType element definition using
typename as a value for the type attribute:
<xs:element name="EmpNumber" type="typename" />
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmpNumber">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
// Additional declarations...
Element Declarations: Complex Type
XML schema classifies the elements that contain attributes and child elements as
complex type elements. The syntax for defining a complex element is:
<xs:element name="elementname">
<xs:complexType>
...
...
</xs:complexType>
where elementname indicates the name of the complex element.
Empty elements, elements that contain only child elements, elements that contain only
simple content, and elements that contain mixed content are classified as complex type
elements.
Similar to simpleType element definitions, you can reuse complexType element
definitions using the following syntax:
<xs:complexType name="typename">
...
...
</xs:complexType>
To reuse the preceding definition, you need to specify typename as a value for the type
attribute in an element declaration. For example, in the following declaration, the
ElementName element reuses the preceding complextype element definition using
typename as a value for the type attribute:
<xs:element name="ElementName" type="typename" />
Complex Type: Attribute Declaration
Complex type elements contain attributes. In an XML schema document, the syntax for
attribute declaration is:
<xs:attribute name="attribute_name" type="data_type"
use="optional/required"/>
where:
· use indicates that the attribute can be optional or required within an element.
An attribute can contain only the data type that is specified while declaring the attribute.
For example, in the code snippet on the right, LocationID is the name of an attribute and
xs:integer indicates that LocationID can contain only numeric values. In addition,
optional indicates that the LocationID attribute is optional within the element declaration.
<xs:schema xmlns:xs="http://www.w3.org/20 01/XMLSchema">
<xs:element name= "DeptDetails">
<xs:complexType>
<xs:attribute name="LocationID" type="xs:integer" use="optional" />
</xs:complexType>
</xs:element>
// Additional declarations...
Complex Type: Empty Elements
An empty element is classified as a complex type element. An empty element cannot
have any content between the start and end tag. However, an empty element can
contain an attribute. For example, the XML document declaration in the code snippet on
the right indicates that the DeptDetails empty element contains a LocationID attribute.
The code snippet on the right also declares the associated XML schema declaration. In
the XML schema declaration, the complexType element indicates that the DeptDetails
element is a complex element. In addition, the declaration indicates that the
complexType element contains the LocationID attribute.
XML Document Declaration
------------------------------------------
<DeptDetails LocationID="123" />
XML Schema Declaration
------------------------------------------
<xs:element name="DeptDetails">
<xs:complexType>
<xs:attribute name="LocationID"
type="xs:integer" />
</xs:complexType>
</xs:element>
Complex Type: Elements That Contain Child Elements
An element that contains only child elements is classified as a complex type element.
For example, the XML document declaration in the code snippet on the right indicates
that the EmpName element contains two simple type elements, firstname and lastname.
The code snippet on the right also declares the associated XML schema declaration. In
this declaration, the complexType element indicates that the EmpName element is a
complex element. In addition, the declaration indicates that the complexType element
contains two simple type elements, firstname and lastname.
Note that the xs:sequence element has been used before the firstname element
declaration. The <xs:sequence> tag declares that the firstname and lastname elements
must appear in the specified order within the DeptDetails element. The <xs:sequence>
tag is an order indicator.
XML Document Declaration
--------------------------------------------
<EmpName>
<firstname>ABC</firstname>
<lastname>DEF</lastname>
</EmpName>
XML Schema Declaration
----------------------------------------
<xs:element name="EmpName">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname"
type="xs:string" />
<xs:element name="lastname"
type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
Order Indicators
Order indicators define the order or sequence in which child elements
can occur in an XML document. The following table lists the order
indicators
Indicat
Description Example
or
sequen Specifies that <xs:element name="EmpName">
ce the <xs:complexType>
complexType <xs:sequence>
element must <xs:element name="firstname"
contain child type="xs:string"/> <xs:element
elements, and name="lastname" type="xs:string"/>
the child </xs:sequence>
elements must </xs:complexType>
appear in the </xs:element>
specified order
The EmpName element must contain
firstname and lastname elements, and the
firstname element must appear before the
lastname element.
all Specifies that <xs:element name="DeptID">
the <xs:complexType>
complexType <xs:all>
element can <xs:element
contain child name="DeptNumber" type="xs:string"/>
elements in any <xs:element name="LocationID"
order, and the type="xs:string"/>
child elements </xs:all>
must occur only </xs:complexType>
once </xs:element>
The DeptID element must contain
DeptNumber and LocationID elements in any
order, and the child elements must appear
only once.
choice Specifies that <xs:element name="Employee">
the <xs:complexType>
complexType <xs:choice>
element can <xs:element name="permanent"
contain either of type="xs:string"/>
the two child <xs:element name="temporary"
elements type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
The Employee element must contain either
the permanent or temporary element.
Occurrence Indicators
In addition to order indicators, occurrence indicators can be specified.
Occurrence indicators specify how often an element can occur within
an element. The default value for an occurrence indicator is 1. The
two types of occurrence indicators are maxOccurs and minOccurs.
The following table describes both.
Summary
An XML schema document describes the structure of an XML document. Every XML
schema document is associated with the XML schema namespace. An XML schema
document describes elements as simple or complex type.
In this module, you learned about: