xml schema

Post on 20-Nov-2014

118 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

XML Schema

The First Step

• After creating the XML Schema document, you must the begin the instance document as you normally would with the XML processing instruction and appropriate root element:

<?xml version=“1.0”?>

<root_element>

<!-– statements go here -->

</root_element>

The Second Step

• After including the XML PI (processing instruction) and root element, you must then specify the XML Schema namespace using the xmlns attribute along with a prefix (xsi in our case) so you can borrow elements and attributes from that namespace:

<?xml version=“1.0”?>

<root_element xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”>

<!-– statements go here -->

</root_element>

The Third Step

• Next you will include the noNamespaceSchemaLocation attribute from the XML Schema namespace along with the prefix that you previously declared (xsi) and include the name of the XML Schema document you created:

<?xml version=“1.0”?>

<root_element xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:noNamespaceSchemaLocation=“filename.xsd”>

<!-– statements go here -->

</root_element>

XML Schema

Getting Started

The First Step

• When creating a XML Schema document, you must begin the document with the XML processing instruction and specify the version of XML that the document is based on:

<?xml version=“1.0”?>

<!-– statements go here -->

The Second Step

• After including the XML PI (processing instruction), you must then initiate the XML schema document using the <schema> element. The remainder of the document will be encased inside this element.

<?xml version=“1.0”?>

<schema xmlns="http://www.w3.org/2001/XMLSchema">

<!-– statements go here -->

</schema>

The Third Step

• Including the <schema> element as we have done is fine but in the case of adding additional namespaces to the document, we need to expand the notation and specify the use of the XML Schema namespace so there is no confusion within the document:

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<!-– statements go here -->

</xsd:schema>

SimpleExample.xsd• <?xml version="1.0" encoding="UTF-8"?>• <xsd:schema

xmlns:xsd="http//www.w3.org/2001/XMLSchema">• <xsd:element name="mytag">• <xsd:complextype>• <xsd:sequence>• <xsd:element name="firstname" type="xsd:string"/>• <xsd:element name="lastname" type="xsd:string"/>• </xsd:sequence>• </xsd:complextype>• </xsd:element>• </xsd:schema>

SimpleExample.xml

• <?xml version="1.0" encoding="UTF-8"?>• <mytag

xmlns:xsi="http://www.w3.org/2001/XMLSchema - instance"

• xsi:noNamespaceSchemaLocation="schema.xsd">

• <firstname>john</firstname>• <lastname>doe</lastname>• </mytag>

XML Schema

Defining the Root Element

Defining the Root Element

• Within the <schema> element, the Root Element of the Schema (Grammar) is defined.

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element/>

<!-– or -->

<xsd:element></xsd:element>

</xsd:schema>

Elements in XML Schema are defined using <element> elements.

Defining a Root Element

• To define an element such as catalog or club-database, you must include the name attribute within the <element> element.

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“element_name”/>

<!-– or -->

<xsd:element name=“element_name”></xsd:element>

</xsd:schema>

An Example

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“club-database”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<!–- statements go here -->

</club-database>

XML Schema

XML File

An Example

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“club-database”></xsd:element>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<!–- statements go here -->

</club-database>

XML Schema

XML File

Defining a Root Element

• In addition to specifying that we are creating an element, we must also specify the type of content it will contain. In the case of a root element, the content type will be COMPLEX and we must nest the <complexType> element within the <element> element.

<xsd:element name=“element_name”>

<xsd:complexType>

<!-- more elements go here -->

</xsd:complexType>

</xsd:element>

An Example

<xsd:element name=“club-database”>

<xsd:complexType>

<!-- more statements go here -->

</xsd:complexType>

</xsd:element>

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<!–- statements go here -->

</club-database>

XML Schema

XML File

Defining a Root Element

• Next we must define the contents of the <complexType> element. Since it would be pointless to have a root-element without children, we must now nest a <sequence> element within the <complexType>.

<xsd:element name=“element_name”>

<xsd:complexType>

<xsd:sequence>

<!-- more elements go here -->

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<!-- more elements go here -->

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<!–- statements go here -->

</club-database>

XML Schema

XML File

Defining a Root Element

• And FINALLY, we can specify the contents that will occur within the root element using another <element> element

<xsd:element name=“element_name”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element1” .../>

<xsd:element name=“child_element2” .../>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” .../>

<xsd:element name=“club” .../>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club/>

<club/>

</club-database>

XML Schema

XML File

An Example

<xsd:element name=“grades-roster”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“course-info” .../>

<xsd:element name=“student” .../>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<?xml version=“1.0” encoding=“UTF-8”?>

<grades-roster>

<course-info/>

<student/>

</grades-roster>

XML Schema

XML File

XML Schema

Defining Children Elements

Defining an Elements Content

• Defining children elements in XML Schema is similar to defining a root element with a few twists. At this point, we will assume that the only datatype available is the ‘string’ datatype which we will use in the following examples.

Defining an Elements Content

• An elements content may be defined or specified in several ways. The first way is to specify it within the <element> element itself using the type attribute.

<xsd:element name=“child-element” type=“content-type”/>

Defining an Element to Contain Text• To define the content of an element to be text

we would specify the type as ‘string’ and since we are using the xsd namespace we would do the following. Note that ‘string’ is case sensitive and must be entered in lowercase.

<xsd:element name=“child-element” type=“xsd:string”/>

Warning: If you forget the namespace specifier, you will get a validation error.

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”/>

<xsd:element name=“club” type=“xsd:string”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

</club-database>

XML Schema

XML File

Defining Multiples

• Elements that can appear zero or more times and elements that can appear one or more times but the way in which we do this is slightly different and involves two new attributes: minOccurs and maxOccurs. Note that the default values for these attributes is ‘1’.

Defining Zero or One Element

• To define an element so that it can be optionally included, you must include attribute minOccurs within the <element> element and specify a value of ‘0’.

<xsd:element name=“element_name”> <!-- Root Element -->

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”

minOccurs=“0”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“0”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database></club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“0”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

</club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“0”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

</club-database>

XML Schema

XML File

This is invalid since the default

value for maxOccurs is ‘1’.

Defining One or More Elements

• To define an element so that it can be optionally included, you must include attribute maxOccurs within the <element> element and specify a value of ‘unbounded’.

<xsd:element name=“element_name”> <!-- Root Element -->

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”

maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

</club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

</club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database></club-database>

XML Schema

XML File

This is invalid since the default value

for minOccurs is ‘1’.

<xsd:element name=“element_name”> <!-- Root Element -->

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”

minOccurs=“0” maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

Defining Zero or More Elements

• To define an element so that it can be included zero or more times, both the minOccurs and maxOccurs attributes must be added along with the values ‘0’ and ‘unbounded’ respectively.

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“0” maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database></club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“0” maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

</club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“0” maxOccurs=“unbounded”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

</club-database>

XML Schema

XML File

<xsd:element name=“element_name”> <!-- Root Element -->

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”

minOccurs=“n” maxOccurs=“n”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

Defining A Specific Number Of Elements

we can specify a specific number of repetitions other than 1 using both the minOccurs and maxOccurs attributes.

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“2” maxOccurs=“2”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 1</club>

</club-database>

XML Schema

XML File

<xsd:element name=“element_name”> <!-- Root Element -->

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”

minOccurs=“n” maxOccurs=“y”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

Defining A Specific Range Of Element Repetitions

• Unlike Relax NG and DTDs, we can specify a specific range of element repetitions other than 0 or more AND 1 or more using both the minOccurs and maxOccurs attributes.

• NOTE: minOccurs must be less than maxOccurs.

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“2” maxOccurs=“3”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

</club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“2” maxOccurs=“3”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

<club>Club 3</club>

</club-database>

XML Schema

XML File

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“2” maxOccurs=“3”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

</club-database>

XML Schema

XML File

This is invalid since the number of clubs

is less than the specified

minOccurs value.

<xsd:element name=“club-database”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”

minOccurs=“2” maxOccurs=“3”/>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<club>Club 2</club>

<club>Club 3</club>

<club>Club 4</club>

</club-database>

XML Schema

XML File

This is invalid since the number of clubs is greater than the

specified maxOccurs value.

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:element name=“child_element” type=“xsd:string”/>

<xsd:element name=“child_element” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

Defining A Choice of Elements

• A choice of one element from a group of two or more elements can be specified within a sequence using a <choice> element

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<member-name>Joe Smith</member-name>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<member-id>99-23455</member-id>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database></club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<member-name>Joe Smith</member-name>

<member-id>99-23455</member-id>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”/>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”/>

<xsd:element name=“child_element” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

</xsd:complexType>

Defining A Group of Elements

• A group of elements can be specified within a sequence using an additional <sequence> element which must appear together.

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:sequence>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-addr” type=“xsd:string”/>

<xsd:sequence>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<member-name>Joe Smith</member-name>

<member-addr>Cupertino, CA</member-addr>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:sequence>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-addr” type=“xsd:string”/>

<xsd:sequence>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<member-id> 99-23455</member-id>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

<xsd:sequence>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-addr” type=“xsd:string”/>

<xsd:sequence>

<xsd:element name=“member-id” type=“xsd:string”/>

</xsd:choice>

</xsd:sequence>

</xsd:complexType>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<member-name>Joe Smith</member-name>

<member-addr>Cupertino, CA</member-addr>

<member-id> 99-23455</member-id>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“child_element” type=“xsd:string”/>

<xsd:sequence minOccurs=“n” maxOccurs=“y”>

<xsd:element name=“child_element” type=“xsd:string”/>

<xsd:element name=“child_element” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

</xsd:complexType>

Defining A Repeating Group of Elements• The minOccurs and maxOccurs attributes may

be added to the <sequence> element to specify a certain number of repetitions.

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”/>

<xsd:sequence minOccurs=“2” maxOccurs=“2”>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-code” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<member-name>Name 1</member-name>

<member-code>Code 1</member-code>

<member-name>Name 2</member-name>

<member-code>Code 2</member-code>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”/>

<xsd:sequence minOccurs=“2” maxOccurs=“2”>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-code” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<member-name>Club 2</member-name>

<member-code>Club 3</member-code>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”/>

<xsd:sequence minOccurs=“2” maxOccurs=“2”>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-code” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<member-name>Name 1</member-name>

<member-code>Code 1</member-code>

<member-code>Code 2</member-code>

<member-name>Name 2</member-name>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”/>

<xsd:sequence minOccurs=“2” maxOccurs=“2”>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-code” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<member-name>Name 1</member-name>

<member-code>Code 1</member-code>

<member-name>Name 2</member-name>

</club-database>

XML Schema

XML File

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“club” type=“xsd:string”/>

<xsd:sequence minOccurs=“2” maxOccurs=“2”>

<xsd:element name=“member-name” type=“xsd:string”/>

<xsd:element name=“member-code” type=“xsd:string”/>

</xsd:sequence>

</xsd:sequence>

An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>

<club>Club 1</club>

<member-name>Name 1</member-name>

<member-code>Code 1</member-code>

<member-code>Code 2</member-code>

</club-database>

XML Schema

XML File

XML Schema

Types of Content

Other Things Besides Strings

• In addition to specifying the content of an element as a string, there is a long list of Data Types that can be specified as a value for the type attribute within the <element> element.

<xsd:element name=“child-element” type=“content-type”/>

Built-In Datatypes

• In addition, the built-in datatypes for XML Schema can also be classified as one of the following:

String Types

Numeric Datatypes

Date and Time Datatypes

String Type

string - text – similar to CDATA in DTDs

An Example: string

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“club-database” type=“xsd:string”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<club-database>Club Database Text</club-database>

XML Schema

XML File

Numeric Datatypes

boolean - true or 1 AND false or 0.

float - IEEE single-precision 32-bit floating point - Special values: 0, -0, INF, -INF, NaN

double - IEEE double-precision 64-bit floating point - Special values: 0, -0, INF, -INF, NaN

decimal - Base type for integers. - Must support a minimum of 18 digits. - Do not support scientific notation.

INF:infinity

NaN:Not a

Number

An Example: boolean

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:boolean”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>true</sample>

XML Schema

XML File

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>1</sample> XML File

An Example: float

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:float”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>-3E2</sample>

XML Schema

XML File

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>6.2</sample> XML File

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>3e-2</sample> XML File

An Example: decimal

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:decimal”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>-3.2</sample>

XML Schema

XML File

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>6.01</sample> XML File

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>2</sample> XML File

Numeric Datatypes

integer - both positive and negative integer values

nonNegativeInteger - integer values starting at 0 through +n

positiveInteger - integer values greater than 0

negativeInteger - integer values less than 0

nonPositiveInteger - integer values starting at 0 through -n

Numeric Datatypes

byte - -128 through 127

short - -32768 through 32767

int - -2147483648 through 2147483647

long - -9223372036854775808 through 9223372036854775807

Numeric Datatypes

unsignedByte - 0 through 255

unsignedShort - 0 through 65535

unsignedInt - 0 through 4294967295

unsignedLong - 0 through 18446744073709551615

Numeric Datatypes

base64Binary

- allows you to include binary data within the XML document using ASCII and EBCDIC characters

hexBinary

- allows you to include hex-encoded binary information within the XML document

An Example: hexBinary

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:hexBinary”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>0FB3</sample>

XML Schema

XML File

Date and Time Datatypes

duration - A time duration;

date - A specific date; [CCYY-MM-DD]

time - A specific time; [hh:mm:ss]

dateTime - A specific instant in time; [CCYY-MM-DDThh:mm:ss] where T is the Date/Time separator

An Example: date

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:date”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>2003-07-29</sample>

XML Schema

XML File

An Example: time

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:time”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>06:58:32</sample>

XML Schema

XML File

An Example: dateTime

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:dateTime”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>2003-07-29T06:58:32</sample>

XML Schema

XML File

<?xml version=“1.0” encoding=“UTF-8”?>

<sample> 2003-07-29T06:58:32-05:00</sample> XML File

Date and Time Datatypes

gYear - A specific Gregorian year; [CCYY]

gMonth - A specific Gregorian month; [--MM--]

gYearMonth - A specific Gregorian month/year; [CCYY-MM]

gDay - A specific Gregorian day; [----DD]

gMonthDay - A specific Gregorian month and day; [--MM-DD]

An Example: gYear

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:gYear”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>2003</sample>

XML Schema

XML File

An Example: gMonth

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:gMonth”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>--07--</sample>

XML Schema

XML File

An Example: gYearMonth

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:gYearMonth”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>2003-07</sample>

XML Schema

XML File

An Example: gDay

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:gDay”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>---29</sample>

XML Schema

XML File

An Example: gMonthDay

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name=“sample” type=“xsd:gMonthDay”/>

<!-- more statements go here -->

</xsd:schema>

<?xml version=“1.0” encoding=“UTF-8”?>

<sample>--07-29</sample>

XML Schema

XML File

Adding Attributes with Content

• To add an <attribute> which requires a text value, would place a type attribute with the string datatype inside the <attribute> element .

<xsd:element name=“catalog”>

<xsd:complexType>

<xsd:attribute name=“catid” type=“xsd:string”/>

</xsd:complexType>

</xsd:element>

<catalog catid=“123”> Catalog of Books </catalog>

Xml processor

• Xml is parsed by a piece of software called an xml parser.

• W3c standard.• Xml parser provides an application with access

to the elements within a document.• Thus it is link between the document and the

application.

Xml Processing

XMLDoc

XMLProcessor Application

Schema

DOM parsers

• A DOM parser reads an entire at once and holds a corresponding tree-like structure in memory.

• Each element within the document is a node in the tree.

A DOM parser’s memory structure• <?xml version =“1.0”

encoding=“utf-8”?>• <AirlineBooking>• <customerName>• <first_name>john</first_name>• <last_name>doe</last_name>• </customerName>• <date>2004-02-02</date>• <airline>United

Airlines</airline>• <flight_no>1234</flight_no>• </AirlineBooking>

AirlineBooking

datecustomerName

Flight_no

Last_name

First_name

airline

SAX parsers

• A SAX parser is an event-driven parser.• It reads the document sequentially without

storing the structure into memory.• SAX is more efficient than DOM.

Event sequence via SAX processing• <?xml version =“1.0”

encoding=“utf-8”?>• <CarRental>• <customerName>• John doe </customerName>• <date>2004-02-02</date>• <model>Oldsmobile

Alero</model>• </CarRental>

• Start Document• Start Element “CarRental”• Start Element “customeName”• Character Data “john Doe”• End Element “customerName”• Start Element “date”• Character Data “2004-02-02”• End Element “date”• Start Element “model”• Character Data “Oldsmobile Alero”• End Element “model”• End Element “CarRental”• End Document

Presenting XML(XSL)• <?xml version="1.0" encoding="UTF-8"?>• <?xml-stylesheet type="text/xsl" href="resulttree.xsl"?>• <people>• <person born="1912">• <name>• <firstname>lalitha</firstname>• <lastname>kavitha</lastname>• </name>• <profession>teaching</profession>• </person>• <person born="1918">• <name>• <firstname>hero honda</firstname>• <lastname>jony</lastname>• </name>• <profession>se</profession>• </person>• </people>

• <?xml version="1.0" encoding="UTF-8"?>• <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">• <xsl:template match="/">• <html >• <head>• <title>sports</title>• </head>• <body>• <table border="1" bgcolor="cyan">• <thead>• <tr>• <th>born</th>• <th>first name</th>• <th>last name</th>• <th>profession</th>• </tr>• </thead>• <xsl:for-each select="people/person">• <tr>• <td><xsl:value-of select="@born"/></td>• <td><xsl:value-of select="name/firstname"/></td>• <td><xsl:value-of select="name/lastname"/></td>• <td><xsl:value-of select="profession"/></td>• </tr>• </xsl:for-each>• </table>• </body>• </html>• </xsl:template>• </xsl:stylesheet>

• <html>• <head>• <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">• <title>sports</title>• </head>• <body>• <table border="1" bgcolor="cyan">• <thead>• <tr>• <th>born</th>• <th>first name</th>• <th>last name</th>• <th>profession</th>• </tr>• </thead>• <tr>• <td>1912</td>• <td>lalitha</td>• <td>kavitha</td>• <td>teaching</td>• </tr>• <tr>• <td>1918</td>• <td>hero honda</td>• <td>jony</td>• <td>se</td>• </tr>• </table>• </body>• </html>

Document Object Model (DOM)

• XML Documents are treated as a tree of nodes• Every item is a "node"• Child elements and enclosed text are

subnodes• W3C DOM Site: http://www.w3.org/DOM/

The W3C XML DOM Objects• Element - An Element• Attribute - An attribute• Text - Text content of an element or attribute• CDATAsection - CDATA section• EntityReference - Reference to an entity• Entity - Indication of a parsed or unparsed entity• ProcessingInstruction - A processing instruction• Comment - Content of an XML comment• Document - The Document object• DocumentType - Reference to the <!DOCTYPE> element• DocumentFragment - Reference to a fragment of a document

XML Document as a Tree of Nodes

• <?xml version="1.0" encoding="UTF-8"?> <DOCUMENT> <GREETING>Hello from XML</GREETING> <MESSAGE>Welcome to Programing XML in Java</MESSAGE> </DOCUMENT>

Levels of DOM• Level 0 - Early versions of DOM in web browsers• Level 1 - It concentrates on the HTML and XML

document models:– http://www.w3.org/TR/REC-DOM-Level-1/

• Level 2 - It defines a set of objects and interfaces for accessing and manipulating document object.http://www.w3.org/TR/DOM-Level-2-Core/

• Level 3 - It will address document loading and saving, as well as content models (such as DTDs and schemas) with document validation support.http://www.w3.org/TR/DOM-Level-3-Core/

top related