Вы находитесь на странице: 1из 62

Module 2

Javascript

XML Schema

XML SCHEMAS

similar to DTD
schemas are used
to define the
structure of the
document

DTDs disadvantages:

The syntax of the DTD


was un-related to XML,
therefore they cannot
be analysed with an
XML processor

DTDs disadvantages:

Difficult to deal
with 2 different
types of syntaxes

DTDs disadvantages:

DTDs does not


support the
datatype of content
of the tag.
All of them are
specified as text

Schema Fundamentals

Schemas can be
considered as a class in
object oriented
programming
XML document as
Instance of Schemas

The XML Schema-3 primary


purposes.

1.

Specifies elements
and attribute of
XML Document

The XML Schema-3 primary


purposes.
2.

Used to specify the


structure of its
instance of XML
document, including
where and how often
the elements may
appear

The XML Schema-3 primary


purposes.

3.

The schema specifies


the datatype of
every element and
attributes of XML

Defining A Schema

Defining A Schema

schema of schemas.
The name of this
namespace is
http://www.w3.org/2001/XMLSche
ma

Defining A Schema

Every schema has schema as


its root element.
This namespace specification
appears as follows:

xmlns:xsd =
http://www.w3.org/2001/XMLSche
ma

Defining A Schema

The name of the namespace


defined by a schema must be
specified with the
targetNamespace attribute of
the schema element.

targetNamespace =
http://cs.uccs.edu/planeSchema

Defining A Schema

schema of schemas.
The name of this
namespace is
http://www.w3.org/2001/XMLSche
ma

Defining A Schema

elementFormDefault
= qualified

Defining A Schema

The default
namespace,
xmlns =
http://cs.uccs.edu/planeSc
hema

Defining A Schema
Instance

Defining A Schema
Instance
default

namespace

An instance document normally


defines its default namespace to be
the one defined in its schema.
For example, if the root element is
planes,

<planes
xmlns=http://cs.uccs.edu/planeSchema
...
>

The second attribute specification


in the root element for
schemaLocation attribute
This attribute is used to name the
standard namespace for instances,
which includes the name
XMLSchema-instance.
xmlns:xsi =
http://www.w3.org/2001/XMLSche
ma-instance

Third, specify the filename of


the schema in which the
default namespace is defined.
This is accomplished with the
schemaLocation attribute,
use values

the namespace of the schema


filename of the schema.

xsi:schemaLocation
=
http://cs.uccs.edu/p
laneSchema/planes.x
sd

Combining everything,
we get

XML schema data types

XML schema data types

2 User-defined
schema data types:

Simple
Complex.

Simple schema data


types

A simple type cannot have


attributes or include nested
elements.
Content is restricted to strings;
It can be one of the types included in
the XML Schema definition (boolean,
string, date, etc.),
It can be a custom type that you can
define yourself.

Defining a Simple Element

<xs:element
name="xxx"
type="yyy"/>

Example

<lastname>Raj</lastna
me>

<xs:element
name="lastname"
type="xs:string"/>

Example

<age>36</age>

<xs:element
name="age"
type="xs:integer"/>

Example

<dateborn>1970-03-27
</dateborn>
<xs:element
name="dateborn"
type="xs:date"/>

The XML Schema

defines 44 data types,


19 primitive
25 Predefined derived.
User-defined derived
type

primitive data types

xs:string,
xs:decimal,
xs:integer,
xs:boolean,
xs:date,
xs:time

Predefined Derived data


types

byte,
long,
decimal,
unsignedInt,
positiveInteger,
NMTOKEN

User-defined derived data


types

defined by specifying
restrictions on an
existing type, which is
then called a base
type.

Simple Type Restrictions

Password element is a
string type, between
six and 12 characters

use the "simpleType"


element to declare the new
data type
Use "restriction element."

the base attribute value to


designate the data type we
will use to create our data
type.

<xs:simpleType
name="passwordType">
<xs:restriction
base="xs:string">
<xs:minLength value=6>
<xs:maxLength value=12>
</xs:resrtiction>
</xs:simpleType>

Element with passwordType


<xs:element name="PW"
type="passwordType" />
Valid XML Document
<PW>dbmagazine</PW>
Invalid XML Document
<PW>db</PW>

restrictions on an existing
type

Employee Number
element is a integer
data type, greater than
or equal to 1,000 and
less than 10,000

<xs:simpleType name="snoType">
<xs:restriction base="xs:int">
<xs:minInclusive value="1000"/>
<xs:maxExclusive value="10000"/>
</xs:restriction>
</xs:simpleType>
The snoType Employee_Number element
declaration is as follows:
<xs:element name="Employee_Number"
type="snoType" />

restrictions on an existing
type

Department element is
a string type, limited to
either "Sales,"
"Development" or
"Service Department

<xs:simpleType name="belongType">
<xs:restriction base="xs:string">
<xs:enumeration value="Sales"/>
<xs:enumeration
value="Development"/>
<xs:enumeration value="Service"/>
</xs:restriction>
</xs:simpleType>
The belongType type Department element
declaration is as follows:
<xs:element name="Department"
type="belongType" />

Default and Fixed Values for Simple


Elements

<xs:element name="color"
type="xs:string"
default="red"/>
<xs:element name="color"
type="xs:string"
fixed="red"/>

Simple Type can have

default attribute:
Fixed attribute

Complex Types Elements

Sequence

All

Ordered Element
Unordered

Choice

Complex Types

minOccurs attributes
maxOccurs attributes

DTD
<!ELEMENT e1 ((e2,e3?)
+|e4)>

Schema
<xs:element name=e1>
<xs:complexType>
<xs:choice>
<xs:sequence maxOccurs=unbounded>
<xs:element name=e2 type=xs:string/>
<xs:element name=e3 type=xs:string
minOccurs=0/>
</xs:sequence>
<xs:element name=e4 type=xs:string >
</xs:choice>
</xs:complexType>
</xs:element>

Displaying XML
Documents

Displaying XML
Documents

Display raw XML


With CSS
With XSLT

Displaying XML with CSS

<?xml-stylesheet type
= "text/css" href =
file.css"?>

XSLT- EXtensible
Stylesheet Language
Transformations

eXtensible Stylesheet Language


(XSL)

consists of three parts:

XSLT - a language for


transforming XML documents
XPath - a language for
navigating in XML documents
XSL-FO - a language for
formatting XML documents

Overview of XSLT

Functional-style
programming
language
The syntactic structure
of XSLT is XML,

The transformation process


used by an XSLT processor

transform XML into XHTML using


XSLT.

The root element


<xsl:stylesheet>
or
<xsl:transform>

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.
w3.org/1999/XSL/Transf
orm">