Vous êtes sur la page 1sur 89

XML

eXtensible Markup
Language

Introduction
XML stands for EXtensible Markup

Language
XML is text based markup language.
XML is a markup language much like HTML
XML was designed to carry data, not to
display data
XML tags are not predefined. You must
define your own tags
XML is designed to be self-descriptive
XML is a W3C Recommendation
An XML document resides in its own file
with an .xml extension.

The Difference between XML and


HTML
XML is not a replacement for HTML.
XML and HTML were designed with different

goals:
XML was designed to transport and store

data, with focus on what data is


HTML was designed to display data, with
focus on how data looks
HTML is about displaying information, while
XML is about carrying information.

Though HTML and XML are part of SGML, there are

some important differences:


HTML is for humans
HTML describes web pages

XML is for computers


XML describes data

Unlike HTML, XML is case sensitive.

For example, <H1> and <h1> are different


XML
tags.
In HTML, you can omit end tags such as </p> or </li>

tags if it is clear from the context where a paragraph or


list item ends. In XML, you can never omit an end tag.

In

XML, elements that have a single tag without a matching end tag
must end in a /, as in <img src="coffeecup.png"/>.
That way, the parser knows not to look for a </img> tag.

In XML, attribute values must be enclosed in quotation marks. In

HTML, quotation marks are optional.


For example,
<applet code="MyApplet.class" width=300 height=300>
is legal HTML but not legal XML.
In XML, you have to use quotation marks: width="300".
In HTML, you can have attribute names without values.

For example,
<input type="radio" name="language" value="Java" checked>.
In XML, all attributes must have values,
such as checked="true" or (ugh) checked="checked".

With XML You Invent Your Own Tags


the XML language has no predefined tags.
The tags used in HTML are predefined.

HTML documents can only use tags defined


in the HTML standard (like <p>, <h1>,
etc.).
XML allows the author to define his/her own
tags and his/her own document structure.
XML is used in many aspects of web
development, often to simplify data storage
and sharing.

XML Separates Data from HTML


If you need to display dynamic data in your

HTML document, it will take a lot of work to


edit the HTML each time the data changes.
With XML, data can be stored in separate
XML files. This way you can concentrate on
using HTML/CSS for display and layout, and
be sure that changes in the underlying data
will not require any changes to the HTML.

XML Simplifies Data Sharing


XML data is stored in plain text format. This

provides a software- and hardwareindependent way of storing data.


This makes it much easier to create data
that can be shared by different
applications.

XML Simplifies Data Transport


One of the most time-consuming

challenges for developers is to exchange


data between incompatible systems over
the Internet.
Exchanging data as XML greatly reduces
this complexity, since the data can be read
by different incompatible applications.

XML Simplifies Platform Changes


XML data is stored in text format. This

makes it easier to expand or upgrade to


new operating systems, new applications,
or new browsers, without losing data.

XML Makes Your Data More


Available
Different applications can access your data,

not only in HTML pages, but also from XML


data sources.

XML is used to Create New Internet


Languages
XHTML
WSDL for describing available web services
WAP and WML as markup languages for

handheld devices
RSS languages for news feeds

XML Documents Form a Tree


Structure
XML documents must contain aroot element. This element is

"the parent" of all other elements.


The elements in an XML document form a document tree. The tree
starts at the root and branches to the lowest level of the tree.
All elements can have sub elements (child elements):
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the
relationships between elements. Parent elements have children.
Children on the same level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in
HTML).

XMLSyntax Rules
All XML Elements Must Have a Closing Tag
XML Tags are Case Sensitive
XML Elements Must be Properly Nested
XML Documents Must Have a Root Element
XML Attribute Values Must be Quoted
Comments in XML:

<!-- This is a comment -->

Identify Root element, child element, sub child elements and attributes in

following XML file. Also draw the tree structure for the same.
<bookstore>
<bookcategory="COOKING">
<titlelang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<bookcategory="CHILDREN">
<titlelang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<bookcategory="WEB">
<titlelang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

XML - Documents
The following image depicts the parts of XML

document.
<?xml version="1.0"?> Document Prolog Section
<contact-info>
<name>Tanmay Patil</name>
<company>ABC Corp</company>
Document

<phone>(011) 123-4567</phone>
Element

</contact-info>
Document Prolog Section: Thedocument
prologcomes at the top of the document,

XML declaration contains details that prepare an XML

processor to parse the XML document. It is optional, but


when used, it must appear in first line of the XML
document.
An XML declaration should abide with the following rules:
If the XML declaration is present in the XML, it must be placed

as the first line in the XML document.


If the XML declaration is included, it must contain version
number attribute.
The Parameter names and values are case-sensitive.
The names are always in lower case.
The order of placing the parameters is important. The correct
order is:version, encoding and standalone.
Either single or double quotes may be used.
The XML declaration has no closing tag i.e. </?xml>

Document Elementsare the building blocks of

XML. These divide the document into a hierarchy


of sections, each serving a specific purpose. You
can separate a document into multiple sections
so that they can be rendered differently, or used
by a search engine. The elements can be
containers, with a combination of text and other
elements.

XML - Elements
XML elements can be defined as building blocks of an XML. Elements

can behave as containers to hold text, elements, attributes, media


objects or all of these.
Each XML document contains one or more elements, the scope of
which are either delimited by start and end tags, or for empty
elements, by an empty-element tag.
<element-name attribute1 attribute2>
....content
</element-name>
element-nameis the name of the element. Thenameits case in the
start and end tags must match.
attribute1, attribute2are attributes of the element separated by
white spaces. An attribute defines a property of the element. It
associates a name with a value, which is a string of characters. An
attribute is written as:
name = "value
An empty element (element with no content) has following syntax:
<name attribute1 attribute2.../>

XML Elements Rules: Following rules are

required to be followed for XML elements:


An elementnamecan contain any alphanumeric

characters. The only punctuation mark allowed in


names are the hyphen (-), under-score (_) and
period (.).
Names are case sensitive. For example, Address,
address, and ADDRESS are different names.
Start and end tags of an element must be
identical.
An element, which is a container, can contain text
or elements.

XML - Attributes
Attributes are part of the XML elements. An element can

have multiple unique attributes. Attribute gives more


information about XML elements. To be more precise, they
define properties of elements. An XML attribute is always
aname-valuepair.
Attributes are used to add a unique label to an element.
<garden>
<plants category="flowers" />
<plants category="shrubs"> </plants>

</garden>
Attributes are used to distinguish among elements of the
same name. When you do not want to create a new element
for every situation. Hence, use of an attribute can add a little
more detail in differentiating two or more similar elements.

Excercise
Identify errors in following XML syntax.
1. <p>This is a paragraph.</p>

<br>
2. <Message>This is incorrect</message>
3. <b><i>This text is bold and italic</b></i>
4. <notedate=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>

5. <?xml version="1.0"?>
<contact-info>
<name>Tanmay Patil</name>
<company>ABC Corp</company>
<phone>(011) 123-4567</phone>
</contact-info>
<contact-info>
<name>Anuradha Patil</name>
<company>XYZ Corp</company>
<phone>(011) 123-4567</phone>
</contact-info>

<note>
<to>Tove</to>
<from>Jani</Ffrom>
<heading>Reminder</heading>
<body>Don't forget me this weekend!
</body>
</note>

Some important notes


Entity References: Some characters have a special

meaning in XML. If you place a character like "<"


inside an XML element, it will generate an error
because the parser interprets it as the start of a new
element. This will generate an XML error:
<message>if salary < 1000 then</message>
To avoid this error, replace the "<" character with
anentity reference:
&lt;
<
less than
<message>if salary&lt;1000
then</message>
&gt;
>
greater than
There are 5 pre-defined entity
&amp;
&
ampersand
&apos;
'
apostrophe
references in XML:
&quot;

"

quotation
mark

Well Formed XML: XML documents that

conform to the syntax rules above are said to be


"Well Formed" XML documents.
White-space is Preserved in XML:
XML does not truncate multiple white-spaces in a

document (while HTML truncates multiple whitespaces to one single white-space):


XML:Hello Tove
HTML:Hello Tove
XML stores a new line as LF.

Excercise
Design an XML document to store information

about a student in an engineering college


affiliated to IU. The information must include
Name, Name of the College, Brach, Year of
Joining, and e-mail id. Make up sample data for 3
students.
Design an XML document to store information
about breakfast menu of a restaurant. The
information must include name, price,
description and calories. Make up sample data
for 3 breakfast items.

Document Type
Definitions (DTD)
A Document Type Definition (DTD) defines

the legal building blocks of an XML


document.
It defines the document structure with a list

of legal elements and attributes.

Why Use a DTD?


With a DTD, each of your XML files can

carry a description of its own format.


With a DTD, independent groups of people
can agree to use a standard DTD for
interchanging data.
Your application can use a standard DTD to
verify that the data you receive from the
outside world is valid.
You can also use a DTD to verify your own
data.

The Building Blocks of XML Documents


Seen from a DTD point of view, all XML
documents (and HTML documents) are
made up by the following building blocks:
1.
2.
3.
4.
5.

Elements
Attributes
Entities
PCDATA
CDATA

Elements
Elements are themain building blocksof both XML

and HTML documents.


Examples of HTML elements are "body" and "table".
Examples of XML elements could be "note" and
"message".
Elements can contain text, other elements, or be
empty.
Examples of empty HTML elements are "hr", "br
Examples:
<body>some text</body>
<message>some text</message>

Attributes
Attributes provideextra information

about elements.
Attributes are always placed inside the
opening tag of an element. Attributes
always come in name/value pairs.
The following "img" element has additional
information about a source file:
<img src="computer.gif" />

Entities
Some characters have a special meaning in

XML, like the less than sign (<) that defines


the start of an XML tag.
Most of you know the HTML entity:
"&nbsp;". This "no-breaking-space" entity is
used in HTML to insert an extra space in a
document. Entities are expanded when a
document is parsed by an XML parser.

The following entities are


predefined in XML:
Entity References : Character
&lt;
<
&gt;
>
&amp;
&
&quot;
"
&apos;
'

PCDATA
PCDATA means parsed character data.
Think of character data as the text found between

the start tag and the end tag of an XML element.


PCDATA is text that WILL be parsed by a
parser.The text will be examined by the
parser for entities and markup.
Tags inside the text will be treated as markup and
entities will be expanded.
However, parsed character data should not
contain any &, <, or > characters; these need to
be represented by the &amp; &lt; and &gt;
entities, respectively.

CDATA
CDATA means character data.
CDATA is text that will NOT be parsed

by a parser.
Tags inside the text will NOT be treated as
markup and entities will not be expanded.

Declaring Elements
In a DTD, elements are declared with an

ELEMENT declaration.

<!ELEMENT element-name category>


or
<!ELEMENT element-name (elementcontent)>

Empty Elements
Empty elements are declared with the

category keyword EMPTY:


<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
XML example:
<br />

Elements with Parsed Character


Data
Elements with only parsed character data

are declared with #PCDATA inside


parentheses:
<!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT from (#PCDATA)>

Elements with Children (sequences)


Elements with one or more children are

declared with the name of the children


elements inside parentheses:
<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name
(child1,child2,...)>
Example:
<!ELEMENT note (to,from,heading,body)>

When children are declared in a sequence

separated by commas, the children must


appear in the same sequence in the
document.
In a full declaration, the children must also
be declared, and the children can also have
children.
The full declaration of the "note" element
is:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>


A DTD can be declared inline inside an XML
document, or as an external reference.

Internal DTD Declaration


If the DTD is declared inside the XML file, it

should be wrapped in a DOCTYPE definition


with the following syntax:
<!DOCTYPE root-element [elementdeclarations]>

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT to (a)>
<!ELEMENT a (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove
<a>eeeeee</a>
</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Hello</body>
</note>

The DTD above is interpreted like this:


!DOCTYPE notedefines that the root element
of this document is note
!ELEMENT notedefines that the note element
contains four elements: "to,from,heading,body"
!ELEMENT todefines the to element to be of
type "#PCDATA"
!ELEMENT fromdefines the from element to
be of type "#PCDATA"
!ELEMENT headingdefines the heading
element to be of type "#PCDATA"
!ELEMENT bodydefines the body element to
be of type "#PCDATA"

External DTD Declaration


If the DTD is declared in an external file, it

should be wrapped in a DOCTYPE definition


with the following syntax:

<!DOCTYPE root-element SYSTEM


"filename">

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Hello</body>
</note>

note.dtd
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Declaring Only One Occurrence of an Element


<!ELEMENTelement-name (child-name)>

Example:
<!ELEMENTnote (message)>
The example above declares that the child element "message" must
occur once, and only once inside the "note" element.
Declaring Minimum One Occurrence of an Element
<!ELEMENTelement-name (child-name+)>

Example:
<!ELEMENTnote (message+)>
The + sign in the example above declares that the child element
"message" must occur one or more times inside the "note" element.
Declaring Zero or More Occurrences of an Element
<!ELEMENTelement-name (child-name*)>

Example:
<!ELEMENTnote (message*)>
The * sign in the example above declares that the child element
"message" can occur zero or more times inside the "note" element.

Declaring Zero or One Occurrences of an Element


<!ELEMENTelement-name (child-name?)>

Example:
<!ELEMENTnote (message?)>
The ? sign in the example above declares that the child element
"message" can occur zero or one time inside the "note" element.
Declaring either/or Content
<!ELEMENTnote (to,from,header,(message|body))>
The example above declares that the "note" element must

contain a "to" element, a "from" element, a "header" element,


and either a "message" or a "body" element.
Declaring Mixed Content
<!ELEMENTnote (#PCDATA|to|from|header|message)*>
The example above declares that the "note" element can contain

zero or more occurrences of parsed character data, "to", "from",


"header", or "message" elements.

Attributes in DTD
In a DTD, attributes are declared with an

ATTLIST declaration.
<!ATTLISTelement-name attribute-name
attribute-type attribute-value>
DTD example:
<!ATTLISTpayment type CDATA "check>
XML example:
<paymenttype="check"/>
Theattribute-typecan be one of the
following:

CDATA

The value is character data

(en1|en2|..)

The value must be one from an enumerated list

ID

The value is a unique id

IDREF

The value is the id of another element

IDREFS

The value is a list of other ids

NMTOKEN

The value is a valid XML name

NMTOKENS

The value is a list of valid XML names

ENTITY

The value is an entity

ENTITIES

The value is a list of entities

NOTATION

The value is a name of a notation

xml:

The value is a predefined xml value

Theattribute-valuecan be one of the

following:The default value of the attribute


value
#REQUIRED

The attribute is required

#IMPLIED

The attribute is optional

#FIXEDvalu
e

The attribute value is fixed

A Default Attribute Value:


DTD:
<!ELEMENTsquare EMPTY>
<!ATTLISTsquare width CDATA "0>
Valid XML:
<squarewidth="100"/>

#REQUIRED:
<!ATTLISTelement-name attribute-name

attribute-type #REQUIRED>
DTD:
<!ATTLISTperson number CDATA #REQUIRED>
Valid XML:
<personnumber="5677"/>
Invalid XML:
<person/>
Use the #REQUIRED keyword if you don't have
an option for a default value, but still want to
force the attribute to be present.

#IMPLIED
<!ATTLISTelement-name attribute-name

attribute-type #IMPLIED>
DTD:
<!ATTLISTcontact fax CDATA #IMPLIED>
Valid XML:
<contactfax="555-667788"/>
Valid XML:
<contact/>
Use the #IMPLIED keyword if you don't want to
force the author to include an attribute, and you
don't have an option for a default value.

#FIXED
<!ATTLISTelement-name attribute-name attribute-type

#FIXED "value">
DTD:
<!ATTLISTsender company CDATA #FIXED "Microsoft>
Valid XML:
<sendercompany="Microsoft"/>
Invalid XML:
<sendercompany=ABC"/>
Use the #FIXED keyword when you want an attribute to
have a fixed value without allowing the author to
change it. If an author includes another value, the XML
parser will return an error.

Enumerated Attribute Values


<!ATTLISTelement-name attribute-name (en1|

en2|..) default-value>
DTD:
<!ATTLISTpayment type (check|cash) "cash>
XML example:
<paymenttype="check"/>
or
<paymenttype="cash"/>
Use enumerated attribute values when you want
the attribute value to be one of a fixed set of
legal values.

Example:

Namespaces
XML Namespaces provide a method to avoid element name conflicts.
In XML, element names are defined by the developer. This often results

in a conflict when trying to mix XML documents from different XML


applications.
This XML carries HTML table information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
This XML carries information about a table (a piece of furniture):
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>

Solving the Name Conflict Using a Prefix:


Name conflicts in XML can easily be avoided using a name prefix.
This XML carries information about an HTML table, and a piece of

furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two
<table> elements have different names.

XML Namespaces - The xmlns


Attribute
When using prefixes in XML, a so-callednamespacefor the prefix must be

defined.
The namespace is defined by thexmlns attributein the start tag of an
element.
The namespace declaration has the following syntax. xmlns:prefix="URI.
<root>
<h:tablexmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:tablexmlns:f="http://www.abccorp.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>

Namespaces can be declared in the elements where they are used or in

the XML root element:


<rootxmlns:h="http://www.w3.org/TR/html4/"
xmlns:f=http://www.abccorp.com/furniture>
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
The purpose is to give the namespace a unique name. However, often
companies use the namespace as a pointer to a web page containing
namespace information.

Default Namespaces
Defining a default namespace for an element saves us from using

prefixes in all the child elements. It has the following syntax:


xmlns="namespaceURI"
This XML carries HTML table information:
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
This XML carries information about a piece of furniture:
<table xmlns="http://www.abccorp.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>

AUniform Resource Identifier(URI) is a

string of characters which identifies an


Internet Resource.
The most common URI is theUniform
Resource Locator(URL) which identifies
an Internet domain address.

7. We are having below XML.

Fan
Ceiling Fan
Table Fan
Fan
English Movies Fan
Hindi Movies Fan
Apply Qualified Namespace to differentiate
Both Fan Tag.

XMLSchema
An XML Schema describes the structure of an XML

document, just like a DTD.


An XML document with correct syntax is called "Well
Formed".
An XML document validated against an XML Schema
is both "Well Formed" and "Valid".
XML Schemas are More Powerful than DTD
XML Schemas are written in XML
XML Schemas are extensible to additions
XML Schemas support data types
XML Schemas support namespaces

XML Schema is an XML-based alternative to DTD:

<xs:elementname="note>

<xs:complexType>
<xs:sequence>
<xs:elementname="to"type="xs:string"/
>
<xs:elementname="from"type="xs:string
"/>
<xs:elementname="heading"type="xs:st
ring"/>
<xs:elementname="body"type="xs:strin
g"/>
</xs:sequence>

Why Use an XML Schema?


With XML Schema, your XML files can carry a

description of its own format.


With XML Schema, independent groups of people can
agree on a standard for interchanging data.
With XML Schema, you can verify data.
XML Schemas Support Data Types
One of the greatest strength of XML Schemas is the

support for data types:


It is easier to describe document content
It is easier to define restrictions on data
It is easier to validate the correctness of data
It is easier to convert data between different data types

Another great strength about XML Schemas is

that they are written in XML:


You don't have to learn a new language
You can use your XML editor to edit your Schema

files
You can use your XML parser to parse your Schema
files
You can manipulate your Schemas with the XML
DOM
You can transform your Schemas with XSLT

XSD Simple Elements


XML Schemas define the elements of your XML files.
A simple element is an XML element that contains only text. It cannot

contain any other elements or attributes.


The text can be of many different types. It can be one of the types
included in the XML Schema definition (boolean, string, date, etc.), or
it can be a custom type that you can define yourself.
You can also add restrictions (facets) to a data type in order to limit its
content, or you can require the data to match a specific pattern.
<xs:element name=pp" type=qq"/>
where pp is the name of the element and qq is the data type of the
element.
XML Schema has a lot of built-in data types. The most common types
are:
xs:string xs:decimal
xs:integer

xs:boolean
xs:date xs:time

Example
<lastname>Refsnes</lastname>

<age>36</age>
<dateborn>1970-03-27</dateborn>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
Default and Fixed Values for Simple Elements
Simple elements may have a default value OR a fixed value

specified.
A default value is automatically assigned to the element
when no other value is specified.
<xs:element name="color" type="xs:string" default="red"/>
<xs:element name="color" type="xs:string" fixed="red"/>

Restrictions on Values:
Restrictions are used to define acceptable values for XML elements or

attributes. Restrictions on XML elements are called facets.


<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Set of Values
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="password">

<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

XSD Complex Elements


A complex element contains other elements and/or

attributes.
There are four kinds of complex elements:
empty elements

<product pid="1345"/>
elements that contain only other elements
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
elements that contain only text
<food type="dessert">Ice cream</food>
elements that contain both other elements and text
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>

<xs:element name="employee">

<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:element name="employee"

type="personinfo"/>
<xs:element name="student"
type="personinfo"/>
<xs:element name="member"
type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname"
type="xs:string"/>
<xs:element name="lastname"
type="xs:string"/>

DisplayingXML with CSS

It is possible to use CSS to format an XML

document.
Create an XML file
Create a style sheet

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>

XSLT
XSLT stands for XSL Transformations
XSLT is the most important part of XSL
XSLT transforms an XML document into

another XML document


XSLT uses XPath to navigate in XML
documents
XPath is a language for navigating XML

Documents.

The root element that declares the document to be an XSL style

sheet is <xsl:stylesheet> or <xsl:transform>.


Note:<xsl:stylesheet> and <xsl:transform> are completely
synonymous and either can be used!
The correct way to declare an XSL style sheet according to the

W3C XSLT Recommendation is:


<xsl:stylesheet version="1.0
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
To get access to the XSLT elements, attributes and features we

must declare the XSLT namespace at the top of the document.


The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to

the official W3C XSLT namespace. If you use this namespace, you
must also include the attribute version="1.0.

Start with a Raw XML Document


We want totransformthe following XML document

("cdcatalog.xml") into XHTML:


<?xml version="1.0"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>

Create an XSL Style Sheet


(Extensible Stylesheet Language)

Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation

template:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Link the XSL Style Sheet to the XML

Document

Add the XSL style sheet reference to your XML

document ("cdcatalog.xml"):
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl
href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
</catalog>

Exercise
8. Create an XML file as below:

<book>
<bookname>
<author>
<price>
<booktype>
Provide css for the above XML file in such a way that
hierarchy is displayed in proper format.
9. Apply XSLT for XML defined in Q.1.View All employees
with their Name and Designation in a table.
10. Apply XSLT for XML defined in Q.2.View All employees
with their Name and Designation in a list.

Vous aimerez peut-être aussi