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

Extensible Markup Language: in This Session, You Will Learn To

This document discusses XML schemas and their use in defining the structure of XML documents. It contains the following key points: 1. XML schemas are used to declare elements, attributes, and their data types to enforce consistency in XML documents. Elements can be simple, containing only text values, or complex, containing child elements and attributes. 2. Attributes in XML schemas are declared similarly to elements. Attribute declarations can be defined locally or globally to enable reuse. The <attribute> element is used to declare attributes and qualify their usage. 3. Global attributes declared outside element definitions can be associated with simple or complex data types to facilitate reuse. Attribute values can be restricted by defining simple data types and using restriction facets.

Uploaded by

ajay_anav
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Extensible Markup Language: in This Session, You Will Learn To

This document discusses XML schemas and their use in defining the structure of XML documents. It contains the following key points: 1. XML schemas are used to declare elements, attributes, and their data types to enforce consistency in XML documents. Elements can be simple, containing only text values, or complex, containing child elements and attributes. 2. Attributes in XML schemas are declared similarly to elements. Attribute declarations can be defined locally or globally to enable reuse. The <attribute> element is used to declare attributes and qualify their usage. 3. Global attributes declared outside element definitions can be associated with simple or complex data types to facilitate reuse. Attribute values can be restricted by defining simple data types and using restriction facets.

Uploaded by

ajay_anav
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 32

Extensible Markup Language

Objectives

In this session, you will learn to:


Create an XML schema
Declare attributes in an XML schema
Identify the need for XML namespaces
Reuse XML schema components
Create groups of elements and attributes in an XML schema

Ver. 1.0 Session 2 Slide 1 of 32


Extensible Markup Language
Elements in XML Schemas

There are two types of elements, simple and complex that


can be defined in a schema.
Simple Element
A simple element does not contain any child elements or
attributes.
It contains only values, such as numbers, strings, and dates.
You can specify restrictions on elements by defining a new
simple data type from an existing data type using facet values.
You can also associate an element with a simple data type.

Let us look at the syntax for declaring a simple element.

Ver. 1.0 Session 2 Slide 2 of 32


Extensible Markup Language
Elements in XML Schemas (Contd.)

<xsd:element name= The name attribute specifies the name of


the element declared.
“element-name"
type="data type" The type attribute specifies the data
min0ccurs="nonNegativeInteger" type of the element declared.
max0ccurs="nonNegativeInteger| minOccurs specifies the minimum
unbounded"/> number of times the element can occur.
maxOccurs specifies the maximum
number of times the element can
appear.

Ver. 1.0 Session 2 Slide 3 of 32


Extensible Markup Language
Elements in XML Schemas (Contd.)

Complex Element
A complex element contains other elements, attributes, and
mixed content.
To declare a complex element, you need to first define a
complex data type.
After defining the complex data type, you can declare a
complex element by associating this data type with the
element.

Let us look at the syntax for declaring a complex element.

Ver. 1.0 Session 2 Slide 4 of 32


Extensible Markup Language
Elements in XML Schemas (Contd.)

<xsd:complexType name="data The complexType element is used to


type name"> declare a new complex data type.
Content model declaration The name attribute specifies the name of
</xsd:complexType> the new complex data type.
The Content model declaration
contains the declaration for the elements
and attributes that make up the content
of the complex type.

Ver. 1.0 Session 2 Slide 5 of 32


Extensible Markup Language
Demo: Creating an XML Schema

Problem Statement:
CyberShoppe, a toy and book store in the United States, sends
its product information from the head office to the branch
offices. The product details must be stored in a consistent
format. Restrictions must be placed on the type of data that
can be saved in the data store, in order to ensure uniformity
and consistency of information.
The product details include the product name, a brief
description, product price, and the available quantity on hand.
The price of the product must always be greater than zero.

Ver. 1.0 Session 2 Slide 6 of 32


Extensible Markup Language
Declaring Attributes in a Schema

Attributes in an XML schema are declared in the same way


as elements.
Declaring attributes in an XML schema facilitates the
assimilation of information for an XML document.
Attribute declarations can be defined in two ways:
Simple type definitions: Facilitates local validation of the
attribute information.
Global attribute declarations: Enables reuse of attributes.

Ver. 1.0 Session 2 Slide 7 of 32


Extensible Markup Language
Attribute Element

In XSD, an attribute for a user-defined element is declared


using the attribute element.
The syntax for declaring an attribute in XSD is:
<attribute name="attributename"
ref="attributename"
type="datatypename" use="value" value="value">
</attribute>
The attribute element contains attributes that are used
to further qualify and restrict the scope and usage of the
user-defined attribute.

Ver. 1.0 Session 2 Slide 8 of 32


Extensible Markup Language
Attribute Element (Contd.)

The attribute element consists of the following


attributes:
name
ref
type
use

Ver. 1.0 Session 2 Slide 9 of 32


Extensible Markup Language
Attribute Element (Contd.)

The attribute element consists of the following


Is used to specify the name of a
attributes: user-defined attribute.
name Must be used when the schema element
ref is the parent element of the attribute
element.
type
Colon (:) should not be included in the
use value of the name attribute.

Ver. 1.0 Session 2 Slide 10 of 32


Extensible Markup Language
Attribute Element (Contd.)

The attribute element consists of the following


attributes:
Is used to refer to a user-defined
name attribute declared either in the same or in
ref any other XSD document.
type
use

Ver. 1.0 Session 2 Slide 11 of 32


Extensible Markup Language
Attribute Element (Contd.)

The attribute element consists of the following


attributes:
name
ref Takes a value that specifies the data
type of the user-defined attribute.
type
use

Ver. 1.0 Session 2 Slide 12 of 32


Extensible Markup Language
Attribute Element (Contd.)

The attribute element consists of the following


attributes:
name
ref
type Specifies the way in which an attribute
can be used in an XML document.
use
Values that can be assigned to the use
attribute are optional, default,
required, and fixed.

Ver. 1.0 Session 2 Slide 13 of 32


Extensible Markup Language
Global Attributes

Global attributes are declared outside all element


declarations.
Global attributes facilitate attribute reusability.
Global attributes can be associated with simple and
complex data types.
Global attributes have the schema element as the parent
element.

Ver. 1.0 Session 2 Slide 14 of 32


Extensible Markup Language
Restricting Attributes Values

In order to restrict values that can be assigned to an


attribute:
Declare the attribute and associate it with a user-defined
simple data type.
Create a simple data type by using the XSD simpleType
element.
Use the XSD restriction element within the simpleType
element to restrict the values that can be assigned to the
elements or attributes that use the simple data type.

Ver. 1.0 Session 2 Slide 15 of 32


Extensible Markup Language
Demo: Declaring Attributes in an XML Schema

Problem Statement:
The Marketing Manager at CyberShoppe sends its product
information from its head office to the branch offices. The
branch offices update this file and send it back to the head
office on a routine basis. The product details must be stored in
a consistent format at all branches. Restrictions must be
placed on the type of data that can be saved in the data store,
to ensure uniformity and consistency of information.
CyberShoppe sells two categories of products, books and toys.
Product details include the product name, a brief description,
product price, and the available quantity on hand. The product
price must always be greater than zero. In addition to these
details, the data store needs to store the category and product
ID.

Ver. 1.0 Session 2 Slide 16 of 32


Extensible Markup Language
Introducing XML Namespaces

In XML, a namespace is a virtual space that is assigned or


recognized by a Uniform Resource Identifier (URI).
A namespace is a string that uniquely identifies the
elements and attributes from different schemas.
A namespace is a unique identifier used to resolve conflicts
between elements that have the same names.
The following guidelines ensure the uniqueness of a URI:
Using a URI that is controlled by the developer.
Using a relative URI.

Ver. 1.0 Session 2 Slide 17 of 32


Extensible Markup Language
Declaring Namespaces

A namespace can be declared in an XSD document by


using the xmlns keyword.
The general form of the xmlns keyword is:
xmlns is the name of the attribute.
xmlns:prefix="URI"
prefix is an optional namespace.

There are two types of namespace declarations:


Default Declaration: Declares a default namespace for a
document without specifying the prefix for a default
namespace.
Explicit Declaration: Enables xmlns keyword to associate a
prefix with a namespace.

Ver. 1.0 Session 2 Slide 18 of 32


Extensible Markup Language
Reusing Components of a Schema

Schemas support a high degree of reusability among other


schemas.
Reusability among other schemas is achieved by using the
include or import elements.

Ver. 1.0 Session 2 Slide 19 of 32


Extensible Markup Language
The include Element

The include element is used to include or refer to an external


schema that is located at a definite address.
The syntax for using the include element is:
Specifies the element ID.
<include id="ID"
schemaLocation="filename Specifies the physical location of
the schema file.
"/>

The include element can have multiple occurrences in an


XSD document.
The schema element is the parent element of the include
element.
The restriction on the usage of this element is that the
containing and contained schema files must belong to the
same target namespace.

Ver. 1.0 Session 2 Slide 20 of 32


Extensible Markup Language
The import Element

The import element performs the same function as the


include element.
The import element access components from multiple
schemas that may belong to different target namespaces.
The syntax for using the import element is:
<import id="ID" Specifies the unique element ID.

namespace="namespace" Specifies a namespace URI to which the


imported schema belongs.
schemaLocation="filena
me"/> Is identical to the value used by the
include element.

Ver. 1.0 Session 2 Slide 21 of 32


Extensible Markup Language
Demo: Reusing XML Schema Components

Problem Statement:
The various products at CyberShoppe are purchased from their
suppliers through their branch offices. The purchase order
details are sent to the head office to generate reports. To
ensure that the data can be accessed across all hardware and
software used at the head office, the branch offices send data
in an XML format. On receiving this data, the head office needs
to verify that all branches have specified the required
information in a consistent format.
The purchase order details sent by the branch offices include
the product ID, order ID, date of the purchase order, name and
address of the supplier, quantity ordered, and price per unit.

Ver. 1.0 Session 2 Slide 22 of 32


Extensible Markup Language
Demo: Reusing XML Schema Components (Contd.)

Problem Statement (Contd.):


The product ID and order ID are used in a number of
documents. The product ID begins with P, followed by three
digits. Similarly, the order ID begins with O, followed by three
digits. These restrictions must be specified at a centralized
location such that they can be applied across multiple
documents.

Ver. 1.0 Session 2 Slide 23 of 32


Extensible Markup Language
Creating Grouped Elements and Attributes

An XML schema defines the following in an XML document:


Elements
Attributes
Child elements
Order of the child elements
Number of child elements
State of the element, whether it is empty or includes text
Data types for the elements and attributes
Default and fixed values for the elements and attributes

Ver. 1.0 Session 2 Slide 24 of 32


Extensible Markup Language
Creating Grouped Elements and Attributes (Contd.)

An XML schema combines related elements and attributes


into groups.
Creating grouped elements and attributes facilitates the
following tasks:
Create a reusable group of elements and attributes.
Select a single element from a group.
Specify the sequence of elements.
XSD provides the following elements to group user-defined
elements and attributes:
sequence
group
choice
all
attributeGroup

Ver. 1.0 Session 2 Slide 25 of 32


Extensible Markup Language
The sequence Element

The sequence element ensures that the elements declared


within the opening and closing tags of this element appear
in a specific order. The following code snippet shows the
usage of the sequence element:
<xsd:sequence>
<xsd:element name="FIRSTNAME"
type="xsd:string"/>
<xsd:element name="LASTNAME"
type="xsd:string"/>
<xsd:element name="DESIG"
type="xsd:string"/>
<xsd:element name="DEPARTMENT"
type="xsd:string"/>
</xsd:sequence>

Ver. 1.0 Session 2 Slide 26 of 32


Extensible Markup Language
The group Element

A set of elements can be grouped together by a common


name in an XML schema, and incorporated into a complex
data type.
The syntax for declaring a group element is:
<group
Specifies the maximum number
maxOccurs="nonNegativeInteg of times a group can occur in
er | unbounded“ the XML document.
minOccurs="nonNegativeInteg Specifies the minimum number
er" name="NCName" of times a group can occur in
ref="QName"> </group> the XML document.
Assigns a name for the group
element.
Refers to a group in a complex
type element

Ver. 1.0 Session 2 Slide 27 of 32


Extensible Markup Language
The choice Element

In XSD, a single option can be selected from multiple


options using the choice element.
The choice element allows only one of the elements
contained in the group to be present within the parent
element.
The syntax for declaring a choice element is:
<choice id="ID"
maxOccurs="nonNegativeInteger|unbounded"
minOccurs="nonNegativeInteger">
</choice>

Ver. 1.0 Session 2 Slide 28 of 32


Extensible Markup Language
The all Element

The all element uses the child elements in any order.


The syntax for using the all element is:
<all maxOccurs="positiveInteger" minOccurs="0|1">
</all>

Ver. 1.0 Session 2 Slide 29 of 32


Extensible Markup Language
The attributeGroup Element

The attributeGroup element enables grouping of


attributes that can be reused with different elements.
The syntax for declaring attributeGroup element is:
<attributeGroup>
attribute1
attribute2
:
</attributeGroup>

Ver. 1.0 Session 2 Slide 30 of 32


Extensible Markup Language
Summary

In this session, you learned that:


The simpleType XSD element allows you to create
user-defined simple data types.
The complexType XSD element allows you to create complex
data types.
The restriction element can be used to specify constraints
on the values that can be stored in elements and attributes.
The attribute element is used to declare an attribute in an
XSD document.

Ver. 1.0 Session 2 Slide 31 of 32


Extensible Markup Language
Summary (Contd.)

The attribute element has the following attributes:


name: Specifies the name of the user-defined attribute.
ref: Contains a reference to a global attribute.
use: Specifies whether the use of the user-defined attribute is
mandatory or optional. In addition, it allows you to specify the
default value for an attribute.
type: Specifies the data type of the attribute.
value: Specifies the default or fixed value for a user-defined
attribute.
The use attribute of the attribute element can take optional,
default, fixed, or required as its value.
A global attribute is used to declare an attribute that is not
associated with any element and can be reused within a
schema.

Ver. 1.0 Session 2 Slide 32 of 32

You might also like