0% found this document useful (0 votes)
109 views

Introduction To XML Schema

Organizations prefer XML schema over DTDs because XML schemas provide an object-oriented approach allowing reuse of components, support built-in data types, and describe element and attribute relations. XML schemas use XML syntax to describe document structure including elements, attributes, data types, and constraints. Unlike DTDs, XML schemas support data types, extensibility through derivation and reuse, and specification of constraints like minimum/maximum values.

Uploaded by

Shallu_Saini
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Introduction To XML Schema

Organizations prefer XML schema over DTDs because XML schemas provide an object-oriented approach allowing reuse of components, support built-in data types, and describe element and attribute relations. XML schemas use XML syntax to describe document structure including elements, attributes, data types, and constraints. Unlike DTDs, XML schemas support data types, extensibility through derivation and reuse, and specification of constraints like minimum/maximum values.

Uploaded by

Shallu_Saini
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 15

Introduction to XML Schema

Organizations prefer XML schema because XML schema provides an object-oriented


approach, which allows reuse of XML schema components, to describe the structure of
XML documents. In addition, XML schemas support built-in data types, namespaces,
and XML syntax that help to describe the relations between the valid elements and
attributes of an XML document.
XML Schema
An XML schema document uses XML schema to describe:

· Elements and attributes that can appear in an XML document

· Child elements, the order of child elements, and the number of child elements

· Data types for elements and attributes

· Default and fixed values for elements and attributes


XML Schema: Features
XML schemas are a more robust alternative to DTDs because XML schemas support:

· 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:

· Derive new data types from existing data types

· Reuse an XML schema in other XML schemas

· Decompose an XML schema into separate files for easy maintenance


XML Schema: XML Syntax
XML schemas support XML syntax. Therefore, you do not need to learn a new
language. For example, the following code snippet declares an element in an XML
schema document:
<xs:element name=”ELEMENT_NAME”>
...
...
</xs:element>
Support for XML syntax also allows an XML parser to process the XML schema similar
to any other XML document. In contrast, a DTD supports different syntax. However,
DTDs can be more straightforward than XML schemas, for example, when document
requirements are simple and you do not need to use data types.
XML Schema: Constraints
Unlike DTDs, XML schemas enable you to specify constraints that define the valid
values for elements and attributes. For example, the code snippet on the right restricts
an EmpAge element to contain values between 0 and 100. In addition, you can specify
regular expression values, such as [a-z], for elements and attributes
<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
Introduction to XML Schema: Structure
The structure of an XML schema document consists of declarations that describe the
valid building blocks of an XML document. XML schema declarations also specify the
constraints that are applicable to the attributes and elements defined in an XML
document.
For example, consider that an organization wants to share XML documents that contain
employee information. The organization decides to create XML schemas to describe the
structure of XML data. In the XML schema, the organization can specify the following
constraints:

· 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 EmpAge element must contain a value less than 100.

· 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.

· xmlns:xs="http://www.w3.org/2001/XMLSchema" indicates an attribute that


defines the XML schema namespace.
The element and attribute declarations follow the schema definition.
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
// Additional declarations
Element Declarations: Simple Type
A simple type element cannot contain child elements or attributes. The simple type
element declaration is:
<xs:element name="element_name" type="data_type"
default/fixed="value" />
where:

· element_name indicates the name of the element.

· 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:

· minInclusive and maxInclusive

· 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:

· attribute_name indicates the name of an attribute.


· data_type indicates the data type specified for the attribute.

· 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.

Indicat Description Example


or
maxOc Specifies the <xs:element name="mail">
curs maximum <xs:complexType>
number of times <xs:sequence>
an element can <xs:element name="from" type="xs:string"/>
occur <xs:element name="to" type="xs:string"
maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The to element can occur a maximum of ten
times and a minimum of one time within the
mail element.
minOcc Specifies the <xs:element name="EmpDetails">
urs minimum <xs:complexType>
number of times <xs:sequence>
an element can <xs:element name="EmpName"
occur type="xs:string"/>
<xs:element name="projects"
type="xs:string"
minOccurs="2" maxOccurs="5" />
</xs:sequence>
</xs:complexType>
</xs:element>
The projects element can occur a minimum
of two times and a maximum of five times
within the EmpDetails element.
Note that order and occurrence indicators are specified only for complex type elements.
Complex Type: Elements That Contain Simple Content
An element that contains simple content is classified as a complex type element. For
example, the XML document declaration in the code snippet on the right indicates that
the EmpID element contains integer content and the country attribute.
The code snippet on the right also declares the associated XML schema declaration. In
this declaration, the complexType element indicates that the EmpID element is a
complex type element. The simpleContent element indicates that the complexType
element contains only text and attributes. A simpleContent element must contain an
extension or a restriction element. The restriction element limits the base data type to
the specified data type. Similarly, an extension element extends the base data type of a
complexType element.
XML Document Declaration
----------------------------------------
<EmpID country="USA">001345</EmpID>
XML Schema Declaration
----------------------------------------
<xs:element name="EmpID">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:integer">
<xs:attribute name="country" type="xs:string"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>

Complex Type: Elements That Contain Mixed Content


An element that contains both text and child elements is considered a complex type
element. For example, the XML document declaration in the code snippet on the right
indicates that the report element contains both text and the 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 report element is a
complex element. The mixed attribute indicates that the complexType element contains
text and elements as content
An element can also contain child elements that contain attributes. To specify such
elements, you need to specify the attribute declaration within the child element
declaration. For example, consider the following XML document declaration:
<Message>
<Sender_Name location=”anyplace”>ABC</Sender_Name>
<Address>XYZ</Address>
</Message>
The following code snippet displays the associated XML schema declaration for the
preceding XML document declaration:
<xs:element name="Message">
<xs:complexType>
<xs:sequence>
<xs:element name="Sender_Name" type="xs:string">
<xs:complexType>
<xs:attribute name="location" type="xs:string" use="optional"/>
</xs:complexType>
XML Document Declaration
------------------------------------------
<report> Please confirm that
<firstname>ABC</firstname>.
<lastname>DEF</lastname> has completed the course.
</report>
XML Schema Declaration
------------------------------------------
<xs:element name="report">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Schema Reference
After creating an XML schema document, it needs to be referenced by an XML
document. The code snippet on the right displays an XML document and its referenced
XML schema document.
Like DTDs, the XML schema document reference is typically positioned after the first
line in the XML document and before the actual document content. The following code
snippet specifies that the schemaLocation attribute can be used in the reference
declaration:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
The schemaLocation attribute specifies the location of the XML schema document. The
following code snippet indicates that the XML schema document exists in the C drive of
the local file system:
xsi:schemaLocation="C:\sharedetails.xsd">
As with DTDs, an XML schema document location can be a remote site. The
schemaLocation can contain a value such as:
http://www.mydomain.com/schemas/sharedetails.xsd
XML Document
------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ShareData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="C:\sharedetails.xsd">
<EmpName/>
<EmpNumber/>
<DeptDetails/>
<DeptID/>
</ShareData>
XML Schema Document
------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ShareData">
<xs:complexType>
<xs:sequence>
<xs:element name="EmpName" type="xs:string"/>
<xs:element name="EmpNumber" type="xs:string"/>
<xs:element name="DeptDetails" type="xs:string"/>
<xs:element name="DeptID" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

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:

· The features of XML schema

· The basic sections of an XML schema document


With this information, you can create an XML schema document.

You might also like