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

XML

1. XML
2. Basic XML Concepts
3. Defining XML Data Formats

XML is not
A replacement for HTML
(but HTML can be generated from XML)
A presentation format
(but XML can be converted into one)
A programming language
(but it can be used with almost any language)
A network transfer protocol
(but XML may be transferred over a network)
A database
(but XML may be stored into a database)

But then what is it?

XML stands for EXtensible Markup Language


XML is a markup language much like HTML
XML was designed to describe 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
XML and HTML were designed with different goals:
XML was designed to describe 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.

But then what is it?

XML is a meta markup language


for text documents / textual data

XML allows to define languages


(applications) to represent text
documents / textual data

Usage of XML
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.
With a few lines of JavaScript code, you can read an external XML file and
update the data content of your web page.

XML Simplifies Data Sharing


In the real world, computer systems and databases contain data in incompatible
formats. XML data is stored in plain text format. This provides a software- and
hardware-independent way of storing data. This makes it much easier to create
data that can be shared by different applications.

Usage of XML
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 Makes Your Data More Available


Different applications can access your data, not only in HTML pages, but also
from XML data sources. With XML, your data can be available to all kinds of
"reading machines

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 by Example
<article>
<author>Gerhard Weikum</author>
<title>The Web in 10 Years</title>
</article>

Easy to understand for human users


Very expressive (semantics along with the data)
Well structured, easy to read and write from programs
This looks nice, but

XML by Example
this is XML, too:
<t108>
<x87>Gerhard Weikum</x87>
<g10>The Web in 10 Years</g10>
</t108>

Hard to understand for human users


Not expressive (no semantics along with the data)
Well structured, easy to read and write from programs

XML by Example
and what about this XML document:
<data>
ch37fhgks73j5mv9d63h5mgfkds8d984lgnsmcns983
</data>

Impossible to understand for human users


Not expressive (no semantics along with the data)
Unstructured, read and write only with special programs

The actual benefit of using XML highly depends


on the design of the application.

Possible Advantages of Using XML

Truly Portable Data


Easily readable by human users
Very expressive (semantics near data)
Very flexible and customizable (no finite tag set)
Easy to use from programs
Easy to convert into other representations
(XML transformation languages)
Many additional standards and tools
Widely used and supported

App. Scenario 1: Content Mgt.

Clients

XML2HTML

XML2WML

XML2PDF

Converters

Database with
XML documents

App. Scenario 2: Data Exchange


Supplier

Buyer
XML
Adapter
Legacy
System
(e.g., SAP
R/2)

XML
(BMECat, ebXML, RosettaNet, BizTalk, )

Order

XML
Adapter
Legacy
System
(e.g.,
Cobol)

XML
Basic XML Concepts
XML Standards by the W3C
XML Documents
Namespaces

XML Standards an Overview


XML Core Working Group:
XML 1.0 (Feb 1998), 1.1 (candidate for recommendation)
XML Namespaces (Jan 1999)
XML Inclusion (candidate for recommendation)

XSLT Working Group:


XSL Transformations 1.0 (Nov 1999), 2.0 planned
XPath 1.0 (Nov 1999), 2.0 planned
eXtensible Stylesheet Language XSL(-FO) 1.0 (Oct 2001)

XML Linking Working Group:


XLink 1.0 (Jun 2001)
XPointer 1.0 (March 2003, 3 substandards)

XQuery 1.0 (Nov 2002) plus many substandards


XMLSchema 1.0 (May 2001)

XML Documents
Whats in an XML document?
XML defines certain rules for its syntax that specify how
to create or structure an XML document.
The syntax used to create an XML document is called
Markup syntax
Declaration
Elements
Attributes
Tree
comments

XML Documents
<?xml version=1.0 encoding=UTF-8
standalone=yes?>
<books>
<book>
<subject standard="class V">History</subject>
<author>D.P.Mishra</author>
<publisher>FGB</publisher>
<price>Rs200/-</price>
</book>

XML Documents
For creating XML document following points should
always in mind
1. XML doc must have starting and ending tags
2. XML tags are case sensitive
3. XML elements must be properly nested
4. XML doc must have one root element.
5. XML attributes values must be enclosed in double
quotes.

A Simple XML Document


<article>
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=1 title=Introduction>
The <index>Web</index> provides the universal...
</section>
</text>
</article>

A Simple XML Document


<article>
Freely definable tags
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=1 title=Introduction>
The <index>Web</index> provides the universal...
</section>
</text>
</article>

A Simple XML Document


Start Tag
<article>
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=1 title=Introduction>
The <index>Web</index> provides the universal...
</section>
</text>
</article>

End Tag

Element

Content of
the Element
(Subelements
and/or Text)

A Simple XML Document


<article>
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=1 title=Introduction>
The <index>Web</index> provides the universal...
</section>
</text>
</article>

Attributes with
name and value

A Simple XML Document

(Freely definable) tags: article, title, author


with start tag: <article> etc.
and end tag: </article> etc.
An Element is the basic building block of an XML
document , content embadded within elements.
<article> ... </article>
Elements have a name (article) and a content (...)
Elements may be nested.
Elements may be empty.
Root element:- a doc must have a single root element,
<?xml version=1.0 encoding=UTF-8 standalone=yes?>
<emp>
..
.
</emp>

Elements in XML Documents


Element content is typically parsed character data (PCDATA:-strings with special
characters, and/or nested elements (mixed content if both).

XML Element
1. Name can start with letters or the underscore(_)
2. After the first character number, characters,- (hyphen), . (period) can be use in
element.
3. Name cannot contain space.
4. Element cannot contain the :
5. There cannot be a space after the opening character(<).
Nested Element:- the elements which contain other elements are known as nested
element.
Empty Element:- written without an end tag.
<items> <item item-id =1 /> </items>

Elements vs. Attributes


Elements may have attributes (in the start tag) that have a name
and
a value, e.g. <section number=1>.
What is the difference between elements and attributes?
Only one attribute with a given name per element (but an
arbitrary number of subelements)
Attributes have no structure, simply strings (while elements can
have subelements)
As a rule of thumb:
Content into elements
Metadata into attributes
Example:
<person born=1912-06-23 died=1954-06-07>

XML Documents as Ordered Trees


article

author

title

text
number=1

Gerhard
Weikum

abstract

title=

In order
The Web
in 10 years

section
The

index
Web

provides

Well-Formed XML Documents

A well-formed document must adher to, among others, the


following rules:
Every start tag has a matching end tag.
Elements may nest, but must not overlap.
There must be exactly one root element.
Attribute values must be quoted.
An element may not have two attributes with the same
name.
Comments and processing instructions may not appear
inside tags.
No unescaped < or & signs may occur inside character
data.

Well-Formed XML Documents

A well-formed document must adher to, among others, the


following rules:
Every start tag has a matching end tag.
Elements may nest, but must not overlap.
Only
well-formed
documents
There must be exactly one root element.
can
bemust
processed
Attribute
values
be quoted. by XML
An element may not parsers.
have to attributes with the same
name.
Comments and processing instructions may not appear
inside tags.
No unescaped < or & signs may occur inside character
data.

Document Type Definition(DTD)


DTD provides a framework to validate XML Documents.
It defines the structure that the XML document must
follow.
DTD provide s common rules to define the structure of the
XML data that have to interchange across the world.
DTD can include in XML document either by inline or
as an external reference.

DTD
<?xml version=1.0
encoding=UTF-8
standalone=yes?>
<!DOCTYPE student[
<!ELEMENT student_info (ad)* >
<!ELEMENT ad (rollno, name, college, brch, year,
emid)>
<!ELEMENT rollno (#PCDATA)>
<student_info>
<!ELEMENT name (#PCDATA)>
<ad>
<!ELEMENT college (#PCDATA)>
<rollno>Rollno : 3012010302</rollno>
<!ELEMENT brch (#PCDATA)>
<name>Name : Kabir</name>
<!ELEMENT year (#PCDATA)>
<college>College : OPJIT</college>
<brch>Branch : CSE</brch>
<!ELEMENT emid (#PCDATA)>
<year>Year : 2008</year>
]>

<emid>Email ID : kabir@hotmail.com</emid>
</ad> </student _info>

Namespaces
<library>
<description>Library of the CS Department</description>
<book bid=HandMS2000>
<title>Principles of Data Mining</title>
<description>
Short introduction to <em>data mining</em>, useful
for the IRDM course
</description>
</book>
</library>

Semantics of the description element is ambigous


Content may be defined differently
Renaming may be impossible (standards!)
Disambiguation of separate XML applications using
unique prefixes

Namespaces

XML Namespace is a mechanism to avoid name


conflicts by differentiating elements or attributes within
an XML document that may have identical names, but
different definitions.
Namespace, including declaration methods, scope,
attribute namespace, and default namespace..
Why is it necessary?
As an example, let's assume we have an RDB with a table
of the following structure (employee Table, section
Table).

Namespaces

So, what happens


What kind of SQL
when this data is
statement
would
expressed in an XML
you create
document?
Let'sto
take
obtain
a list of
two
XML documents,
and
see how we
can
Employee
ID,
create
a list showing
Employee
employee ID,
Department
employee department
Name, and
and employee name
Employee
Name?
using XML.?

Name space
<section>
<sectionInfo>
<secID>S001</secID>
<name>Sales</name>
</sectionInfo>
<sectionInfo>
<secID>S002</secID>
<name>Development</nam
e>
</sectionInfo>
</section>
Section XML
Document

<employeeList> <personList>
<empID>E0000001</empID>
<name>Sales</name>
<name>John Smith</name>
</personList>
<personList>
<empID>E0000002</empID>
<name>Development</nam>
<name>Ichiro
Tanaka</name>
</personList>
</employeeList>

employeeListXML Document

<employee> <personInfo>
<empID>E0000001</empID>
<secID>S001</secID>
<name>John Smith</name>
</personInfo> <personInfo>
<empID>E0000002</empID>
<secID>S002</secID>
<name>Ichiro Tanaka</name>
</personInfo>
</employee>

Employee XML Document

Name space

Write a namespace declaration according to the following


description method, describing the element start tag:

If the element and/ or attribute belong to a namespace, a


colon (":") is placed between the namespace prefix and
the element name/ attribute name
As a test, let's take the previous employeeList XML
document as an example, and provide a namespace
declaration and an element belonging to the namespace.

Name space

<emp:employee xmlns:emp="urn:corp:emp">
<emp:personInfo>

.
</emp:personInfo>
</emp:employee>

In this example, we have declared the namespace prefix as "emp", and the
namespace identifier (URI) as "urn:corp:emp". This means that element names
and attribute names with the "emp" prefix (including the employee element) all
belong to the urn:corp:emp namespace

Namespace Syntax
<dbs:book xmlns:dbs=http://www-dbs/dbs>

Prefix as abbrevation
of URI

Unique URI to identify


the namespace

Signal that namespace


definition happens

<sec:section
xmlns:sec="urn:corp:sec">
<sec:sectionInfo>
<sec:secID>S001</sec:secID>
<sec:name>Sales</sec:name>
</sec:sectionInfo>
<sec:sectionInfo>
<sec:secID>S002</sec:secID>
<sec:name>Development</sec:n
ame> </sec:sectionInfo>
</sec:section>
Section XML
Document

<emp:employee
xmlns:emp="urn:corp:emp">
<emp:personInfo>
<emp:empID>E0000001</emp
:empID>
<emp:secID>S001</emp:secI
D> <emp:name>John
Smith</emp:name>
</emp:personInfo>
<emp:personInfo>
<emp:empID>E0000002</emp
:empID>
<emp:secID>S002</emp:secI
D> <emp:name>Ichiro
Tanaka</emp:name>
</emp:personInfo>
</emp:employee>
Employee XML Document

<list:employeeList xmlns:list="urn:corp:list"
xmlns:emp="urn:corp:emp"
xmlns:sec="urn:corp:sec"> <list:personList>
<emp:empID>E0000001</emp:empID>
<sec:name>Sales</sec:name> <emp:name>John
Smith</emp:name> </list:personList>
<list:personList>
<emp:empID>E0000002</emp:empID>
<sec:name>Development</sec:name>
<emp:name>Ichiro Tanaka</emp:name>
</list:personList> </list:employeeList>>

employeeListXML Document

Default Namespace

A "default namespace" is a namespace declaration that


does not use a namespace prefix .

The scope of the default namespace is the element for which


the namespace was declared and the related content, just
as with the namespace scope discussed earlier.
The benefit of using a default namespace is that the
namespace prefix can be omitted.

Default Name Space.

example

Defining XML Data Formats

Document Type Definitions


XML Schema

Document Type Definitions

Sometimes XML is too flexible:


Most Programs can only process a subset of all possible
XML applications
For exchanging data, the format (i.e., elements,
attributes and their semantics) must be fixed
Document Type Definitions (DTD) for establishing the
vocabulary for one XML application (in some sense
comparable to schemas in databases)
A document is valid with respect to a DTD if it conforms
to the rules specified in that DTD.
Most XML parsers can be configured to validate.

DTD Example: Elements


<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT

article
(title,author+,text)>
title
(#PCDATA)>
author
(#PCDATA)>
text
(abstract,section*,literature?)>
abstract
(#PCDATA)>
section
(#PCDATA|index)+>
literature (#PCDATA)>
index
(#PCDATA)>
Content of the text element may
the title element contain zero or more section

Content of
is parsed character data

elements in this position

Content of the article element is a title element,


followed by one or more author elements,
followed by a text element

Element Declarations in DTDs


One element declaration for each element type:
<!ELEMENT element_name content_specification>

where content_specification can be


(#PCDATA) parsed character data
(child)
one child element
(c1,,cn) a sequence of child elements c1cn
(c1||cn)
one of the elements c1cn
For each component c, possible counts can be specified:

c
exactly one such element
c+ one or more
c* zero or more
c? zero or one

Plus arbitrary combinations using parenthesis:


<!ELEMENT f ((a|b)*,c+,(d|e))*>

More on Element Declarations

Elements with mixed content:


<!ELEMENT text (#PCDATA|index|cite|glossary)*>

Elements with empty content:


<!ELEMENT image EMPTY>

Elements with arbitrary content (this is nothing for


production-level DTDs):
<!ELEMENT thesis ANY>

Attribute Declarations in DTDs

Attributes are declared per element:


<!ATTLIST section number CDATA #REQUIRED
title CDATA #REQUIRED>

declares two required attributes for element section.


element name

attribute name
attribute type
attribute default

Attribute Declarations in DTDs

Attributes are declared per element:


<!ATTLIST section number CDATA #REQUIRED
title CDATA #REQUIRED>

declares two required attributes for element section.


Possible attribute defaults:
#REQUIRED
is required in each element instance
#IMPLIED
is optional
#FIXED default always has this default value
default
has this default value if the attribute is
omitted from the element instance

Attribute Types in DTDs

string data
(A1||An)
enumeration of all possible values of the
attribute (each is XML name)
ID
unique XML name to identify the element
IDREF refers to ID attribute of some other element
(intra-document link)
IDREFS
list of IDREF, separated by white space
plus some more
CDATA

Attribute Examples
<ATTLIST publication type (journal|inproceedings) #REQUIRED
pubid ID #REQUIRED>
<ATTLIST cite
cid
IDREF #REQUIRED>
<ATTLIST citation
ref
IDREF #IMPLIED
cid
ID #REQUIRED>
<publications>
<publication type=journal pubid=Weikum01>
<author>Gerhard Weikum</author>
<text>In the Web of 2010, XML <cite cid=12/>...</text>
<citation cid=12 ref=XML98/>
<citation cid=15>...</citation>
</publication>
<publication type=inproceedings pubid=XML98>
<text>XML, the extended Markup Language, ...</text>
</publication>
</publications>

Attribute Examples
<ATTLIST publication type (journal|inproceedings) #REQUIRED
pubid ID #REQUIRED>
<ATTLIST cite
cid
IDREF #REQUIRED>
<ATTLIST citation
ref
IDREF #IMPLIED
cid
ID #REQUIRED>
<publications>
<publication type=journal pubid=Weikum01>
<author>Gerhard Weikum</author>
<text>In the Web of 2010, XML <cite cid=12/>...</text>
<citation cid=12 ref=XML98/>
<citation cid=15>...</citation>
</publication>
<publication type=inproceedings pubid=XML98>
<text>XML, the extended Markup Language, ...</text>
</publication>
</publications>

Linking DTD and XML Docs

Document Type Declaration in the XML document:


<!DOCTYPE article SYSTEM http://www-dbs/article.dtd>

keywords

Root element

URI for the DTD

Linking DTD and XML Docs

Internal DTD:
<?xml version=1.0?>
<!DOCTYPE article [
<!ELEMENT article (title,author+,text)>
...
<!ELEMENT index (#PCDATA)>
]>
<article>
...
</article>

Both ways can be mixed, internal DTD overwrites


external entity information:
<!DOCTYPE article SYSTEM article.dtd [
<!ENTITY % pub_content (title+,author*,text)
]>

Flaws of DTDs

No support for basic data types like integers, doubles,


dates, times,
No structured, self-definable data types
No type derivation
id/idref links are quite loose (target is not specified)
XML Schema

XML Schemas

Schemas is a general term--DTDs are a form of XML


schemas
According to the dictionary, a schema is a structured
framework or plan

When we say XML Schemas, we usually mean the


W3C XML Schema Language
This is also known as XML Schema Definition language, or
XSD
Ill use XSD frequently, because its short

DTDs, XML Schemas, and RELAX NG are all XML


schema languages

54

Why XML Schemas?

DTDs provide a very weak specification language


You cant put any restrictions on text content
You have very little control over mixed content (text plus
elements)
You have little control over ordering of elements

DTDs are written in a strange (non-XML) format


You need separate parsers for DTDs and XML

The XML Schema Definition language solves these


problems
XSD gives you much more control over structure and content
XSD is written in XML

55

Why not XML schemas?

DTDs have been around longer than XSD


Therefore they are more widely used
Also, more tools support them

XSD is very redundant, even by XML standards


More advanced XML Schema instructions can be nonintuitive and confusing
Nevertheless, XSD is not likely to go away quickly

56

Different between DTD and XML Schema

DTD
Define elements ,
attributes and entities
Does not support Name
spaces
Does not follow the
XML syntax
Lacks strong typing
capabilities

Schema
Define the structure of
an element with more
feature such as data
type and conditions.
Provide support Name
spaces
Follow the XML
syntax
Support simple and
complex data types

XML Schema Basics

XML Schema is an XML application


Provides simple types (string, integer, dateTime,
duration, language, )
Allows defining possible values for elements
Allows defining types derived from existing types
Allows defining complex types
Allows posing constraints on the occurrence of elements
Allows forcing uniqueness and foreign keys

Schema: types of content

Simple Type text only


Complex Types contain other elements or attributes.

Simple Type

Date
Integer
String
Customer built types
In DTD we used #PCDATA to capture a name, a date or
practically anything.

Complex Type

Describe the structure of a document, rather than


content.
Complex Types

Elements that contain elements


Elements that contain element and text
Elements that contain only text
Elements that are empty
Each may contain attributes

Examples of Built-in Simple Types

<xsd:element name=weight type=xsd:string/>

<xsd:element name=population type=xsd:integer />

Custom Simple Type

<xsd:simpleType name=zipcodeType>
<xsd:restriction base=xsd:string>
<xsd:pattern value=\d{5}(-\d{4}? />
</xsd:restriction>
<xsd:simpleType>
This type limits the content of elements (zipcodeType)
to a string with 5 digits, an optional hyphen and 4 extra
digits.

Complex Type Definition

<xsd:complexType name=endType>
<xsd:sequence>
<xsd:element name=animal
type=animalType minOccurs=1
maxOccurs=unbounded/>
</xsd:sequence>
<xsd:complexType>
Defines endType. It contains another element
animal defined with another complex type
animalType to define a particular element

Defining Simple Types


When declaring an element, you choose its name and what kind of content it
should contain.
xsd:string string of characters
xsd:decimal decimal number
xsd:boolean true of false
xsd:date CCYY-MM-DD
xsd:time xsd:uriReference - element will contain an URL
xsd:language two letter language listed in ISO639
Custom name of a custom simple type.
Code.xsd
<xsd:element name="weight" type="xsd:string" />
<xsd:element name="population" type="xsd:integer" />
Code.xml
<weight>500 pounds</weight>
<population>28</population>
When a value is an integer or a string?

Date and Time Data Types

Code.xsd
<xsd:element name=gestation
type=xsd:timeDuration/>
(represent a certain amount of time)
Code.xml
<gestation>P3M15D</gestation>
(PnYnMnDTnHnMnS)

n - non negative
Period

T begins optional time

Date and Type Data Types

Code.xsd
xsd:element name=bedtime type=xsd.time:/>
Code.xml
<bedtime>20:15:05-05:00>/bedtime>

Additional xsd:time

xsd:time (hh:mm:ss.sss)
xsd:timeInstant (CCYY-MM-DDTh:mm:ss.sss)
xsd:date CCYY-MM-DD
xsd:month (CCYY-MM
xsd:year (CCYY)
xsd:century (CC)
xsd:recurringDate
xsd:recurringDay

Number Type

xsd:decimal
xsd:integer
xsd:positiveinteger
xsd:negativeinteger
xsd:float
xsd:double

Simplified XML Schema Example


<xs:schema>
<xs:element name=article>
<xs:complexType>
<xs:sequence>
<xs:element name=author type=xs:string/>
<xs:element name=title type=xs:string/>
<xs:element name=text>
<xs:complexType>
<xs:sequence>
<xs:element name=abstract type=xs:string/>
<xs:element name=section type=xs:string
minOccurs=0 maxOccurs=unbounded/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Function of XML Schema

defines elements that can appear in a document


defines attributes that can appear within elements
defines which elements are child elements
defines the sequence in which the child elements can appear
defines the number of child elements
defines whether an element is empty or can include text
defines default values for attributes

The purpose of a Schema is to define the legal building blocks of


an XML document, just like a DTD.

Example: Shipping Order


<?xml version="1.0"?>
<shipOrder>
<shipTo>
<name>Svendson</name>
<street>Oslo St</street>
<address>400 Main</address>
<country>Norway</country>
</shipTo>

<items>
<item>
<title>Wheel</title>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Cam</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</items>
</shipOrder>

XML Schema for Shipping Order


<xsd:schema xmlns:xsd=http://www.w3.org/1999/XMLSchema>
<xsd:element name="shipOrder" type="order"/>
<xsd:complexType name="order">
<xsd:element name="shipTo" type="shipAddress"/>
<xsd:element name="items" type="cdItems"/>
</xsd:complexType>
<xsd:complexType name="shipAddress">
<xsd:element name="name type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
</xsd:complexType>

XML Schema - Shipping Order

<xsd:complexTypename="cdItems">
<xsd:elementname="item"minOccurs="0"
maxOccurs="unbounded"type="cdItem"/>
</xsd:complexType>
<xsd:complexTypename="cdItem">
<xsd:elementname="title"type="xsd:string"/>
<xsd:elementname="quantity
type="xsd:positiveInteger"/>
<xsd:elementname="price"type="xsd:decimal"/>
</xsd:complexType>
</xsd:schema>

XML for Beginners


Querying XML Data
Xpath
XSLT
XQuery

XPath
XPath is the solution to finding information in an XML
document. XPath uses expressions to find elements, attributes,
and other information in XML file . For example If an XML
document that contained a bunch of favorite books, each with
author children elements, by XPath expression we ncan find all
the authors of your favorite books!
XPath is a syntax for defining parts of an
XML document
XPath uses path expressions to navigate in
XML documents
XPath contains a library of standard functions
XPath is a major element in XSLT
XPath is also used in XQuery, XPointer and
XLink
XPath is a W3C recommendation

XPath expressions

XPath can locate any type of information in an XML


document with one line of code. These one liners are
referred to as "expressions,
An XPath expression is exactly that; it's a line of code
that we use to get information from our XML document.
An XPath expression describes the location of an
element or attribute in our XML document. By starting
at the root element, we can select any element in the
document by carefully creating a chain of children
elements. Each element is separated by a slash "/".

Abc.xml
<inventory>
<drink>
<lemonade supplier="mother" id="1">
<price>$2.50</price>
<amount>20</amount>
</lemonade>
<pop supplier="store" id="2">
<price>$1.50</price>
<amount>10</amount>
</pop>
</drink>
<snack>
<chips supplier="store" id="3">
<price>$4.50</price>
<amount>60</amount>
<calories>180</calories>
</chips>
</snack>
</inventory>

XPath expressions ..continued

For example, if we wanted to know the number of chips


we have in stock(element amount) in abc.xml, the
XPath expression would be:
inventory/snack/chips/amount

1. We specified the root node, inventory, at the


beginning of our XPath expression.
2. We chose inventory's child element, snack,
because it is on the pathway toward our goal,
"number of chips in stock".
3. We chose snack's child element chips.
4. Finally, we chose chips' child element amount,
or in other words, "number of chips in stock".
inventory/snack/chips/amount

Elements of XPath
An XPath expression usually is a location path that
consists of location steps, separated by /:
/article/text/abstract:

selects all abstract elements

A leading / always means the root element


Each location step is evaluated in the context of a node
in the tree, the so-called context node
Possible location steps:

child element x: select all child elements with name x


Attribute @x: select all attributes with name x
Wildcards * (any child), @* (any attribute)
Multiple matches, separated by |: x|y|z

XSLT

XSLT (eXtensible Stylesheet Language


Transformations) is the recommended style sheet
language for XML.
XSLT is far more sophisticated than CSS. With XSLT
elements and attributes can add/remove to or from the
output file. elements can also rearrange and sort,
perform tests and make decisions about which are to be
hide or display.
XSLT uses XPath to find information in an XML
document.
We can say that it is a transformation process :to
transform an XML source document in to an result
document.

XSLT Element

Xsl : template this element define a template or set


of rules to produce output .it uses match attribute for
difining different names and pattern for node
<xsl: template
name=name match =Mode priority=number>
<!- content-- >
<xsl:template>
After defining a template calling of the template is
required to do so either xsl:apply-templates or xsl:calltemplate element are required

Attributes of The xsl:template

Match(optional) value(pattern) : specify which node is


eligible to be processed . If this attribute is absent, then
the name attribute must be present .
Name (optional) vlaue(Name) : Specific name for
template If this attribute is absent then there must be a
match attribute
Priority (optional) value( number)
Mode(optional) value (list of mode name)
<xsl: template name=name match=pattern
mode=mode priority=number>
<!- ->
</xsl:template>

xsl:apply-templates

Define a set of nodes to be processed by selecting an


appropriate template rule. This element selects a set of
nodes and processes each node by finding a matching
template. If the xsl:sort element is nested within xsl:
apply template element then it determines the order in
which the nodes are processed; otherwise, the nodes are
processed in the order they are specified in the
document
Syntax:<xsl:apply-templates select =expression mode=mode>
<!-..->
</xsl:apply-templates>

Example:

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


<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
<student>Jack</student>
<student>Harry</student>
<student>Rebecca</student>
<teacher>Mr. Bean </teacher>
</class>

<?xml version="1.0" ?>


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="teacher">
<p><u><xsl:value-of select="."/></u></p>
</xsl:template>
<xsl:template match="student">
<p><b><xsl:value-of select="."/></b></p>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

output

Explanation

<html>
<body>
<p><b>Jack</b></p>
<p><b>Harry</b></p>
<p><b>Rebecca</b></p>
<p><u>Mr. Bean</u></p>
</body>
</html>

XLink

Xlink language allows elements to be inserted into XML


documents in order to create and describe links between
resources.
Define link relationships among more than two
resources.
Associate metadata with a link
Define links to associate resources not in the document
containing the link

XLink Scope

Element Conformance

Application Conformance

Element Conformance

An element contains a type attribute from the XLink


namespace with a value of arc, extended, locator,
resource, simple, title, or none.
An element adheres to the specified constraints for the
type.
XLink does not define elements.
It defines attributes and how the
containing elements are to be used.

Application Conformance

The recommendation describes processing that an


application must do to conform, and, if optional features
are implemented, what must be done to make the
optional features conform.

XLink Element Types

Links:
simple
extended

Resources (used in extended links)


resource (local resource)
locator (remote resource)

arc (used in extended links)


title (may be in a child element of an extended-, locator- or arc-type element)
none

Link - simple
Defines an outbound link from a local resource to a remote
resource.
Similar to HTML references
<a href=uri> </a> or <img href=uri>

Attributes

href URI of a resource. Xpointer support is explicitly expected.


role URI of a resource that describes the role of the element
arcrole URI of a resource that describes the role of the link
show describes how the href resource is to be presented on
transversal
actuate describes when the transversal to href resource is to happen
title a human readable description of the element

Simple Link Example


XML Document
<doc
xmlns:xlink=/xlink>

<mytag
xlink:type=simple
xlink:role=role_uri
xlink:href=ref_uri
xlink:show=new
xlink:actuate=onLoad>

</mytag>

</doc>

Role definition
Linked resource
Instruction to open the link in a new
window when the link is traversed

Instruction to traverse the link when


loading the original document

Link - extended

An extended link associates an arbitrary number of


resources.
An extended link may be stored separately from all
resources that are being associated.
Enables linking of objects that have no provision of
internal links.
A link element with XLink type=extended contains a
set of elements with XLink types of
resource, locator, arc, and title.

resource

An element with XLink type=resource defines a local


resource.
Attributes
role URI of a resource that describes the role of the element
title a human readable description of the element
label provides a label to which an arc type element may
refer

locator

An element with XLink type=locator defines a remote


resource.
Attributes

href URI of a resource (required)


role URI of a resource that describes the role of the element
title a human readable description of the element
label provides a label to which an arc type element may
refer

arc

An element with XLink type=arc defines links


between (or among) resources. The links can be:
between a location in the document and a remote resource (an
outbound arc)
between a remote resource and a location in the document (an
inbound arc)
between remote resources (a remote arc)

arc

Attributes
from specifies the label of starting resources for a transversal
to specifies the label of ending resources for a transversal
show describes how the href resource is to be presented on
transversal
actuate describes when the transversal to href resource is to
happen
arcrole URI of a resource that describes the role of the arc
title a human readable description of the element

title

An element with XLink type=title permits more


information than a title attribute alone would. A title
element must be a child of the element it supplies title
information for.
An element with XLink type=title only has XLink
specified meaning if it is the child of an element with an
XLink type of extended, locator, or arc.

Behavior attribute

Define how the link is activated , and what the


application should do with the linked resource. It
contain two attributes show an acute
Show attribute define how the remote resource is to be
appear to the user this attribute have 3 option : new,
parsed and replace
The new option specifies that the remote resource must
be shown in a new window. The parsed option specifies
that the content must be integrated into XML document
from where the link is actuated. The replace option
replaces the old document with the linked document.

Semantic attribute

Provide important information that an application ma


require such as the function of a link and its purpose.
The semantic attribute has two attributes role and the
title . The role attribute describes the function of the
links content. The title attributes describes the link in a
huam readable format.

extended - Link

Example
XML Document
<doc xmlns:xlink=/xlink>
<order xlink:type=extended >
<car xlink:type=resource xlink:label=c1>Porsche </car>
<car xlink:type=resource xlink:label=c1>Ferrari </car>
<account xlink:type=locator xlink:label=a1
xlink:href=http://www.chargeme.com/cgi/citibank-12-3123
xlink:title=Brian Temple/>
<location xlink:type=resource xlink:label=d1>
<attn>Matthias Hauswirth</attn>
<street></street> <zip>80303</zip>
</ location >
<charge_to xlink:type=arc xlink:from=c1 xlink:to=a1
xlink:title=Brian pays/>
<deliver_to xlink:type=arc xlink:from=c1 xlink:to=d1/>
</order>
</doc>

Bank
Account

XPointer

The Xpointer language allws hyperlinks to point to


specific parts of XML Document. It uses Xpath
expression to navigate in XML doc.
It is divided into four specifications
1. A Frame work that identifies XML Fragments
2. A positional element addressing a scheme
3. A Scheme for namespace
4. A Scheme for Xpath based addressing

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


<dogbreeds>
<dog breed="Rottweiler" id="Rottweiler">
<picture url="http://dog.com/rottweiler.gif" />
<history>The Rottweiler's ancestors were probably Roman
drover dogs.....</history>
<temperament>Confident, bold, alert and imposing, the Rottweiler
is a popular choice for its ability to protect....</temperament>
</dog>
<dog breed="FCRetriever" id="FCRetriever">
<picture url="http://dog.com/fcretriever.gif" />
<history>One of the earliest uses of retrieving dogs was to
help fishermen retrieve fish from the water....</history>
<temperament>The flat-coated retriever is a sweet, exuberant,
lively dog that loves to play and retrieve....</temperament>
</dog>
</dogbreeds>

So, instead of linking to the entire document (as with XLink), XPointer
allows you to link to specific parts of the document.
To link to a specific part of a page, add a number sign (#) and an
XPointer expression after the URL in the xlink:href attribute,
like this:
xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"
The expression refers to the element in the target document, with the id
value of "Rottweiler
XPointer also allows a shorthand method for linking to an element
with an id. You can use the value of the id directly, like this:
xlink:href="http://dog.com/dogbreeds.xml#Rottweiler".

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


<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
<mydog>
<description>
Anton is my favorite dog. He has won a lot of.....
</description>
<fact xlink:type="simple" xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">
Fact about Rottweiler
</fact>
</mydog>
<mydog>
<description>
Pluto is the sweetest dog on earth......
</description>
<fact xlink:type="simple" xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">
Fact about flat-coated Retriever
</fact>
</mydog>
</mydogs>

XML Parser

An XML Parser is a s/w which read the XML document


and tests whether the document is Well formed against
the given DTD /XML Schema or not.
An XML Parser is used to read , update , create and
manipulate an XML document.
To manipulate the xml document ,the XML parser loads
the document into the computers memory and then
manipulate it using DOM(Document Object Model)
node tree structure.
XML Parser are divided into two category
Non validating parser
Validating Parser

AJAX
1. XML
2. Working of AJAX

AJAX
The term AJAX (Asynchronous JavaScript and XML) has been
around for three years created by Jesse James Garrett in 2005.
The technologies that make Ajax work, however, have been
around for almost a decade. Ajax makes it possible to update a
page without a refresh. Using Ajax, we can refresh a particular
DOM object without refreshing the full page.
In Jesse Garretts original article that coined the term, it was
AJAX. The X in AJAX really stands for XML Http Request
though, and not XML. Jesse later conceded that Ajax should be a
word and not an acronym and updated his article to reflect his
change in heart. So Ajax is the correct casing. As its name
implies, Ajax relies primarily on two technologies to work:
JavaScript and the XMLHttpRequest. Standardization of the
browser DOM (Document Object Model) and DHTML also play

How Ajax Works


Magical ability of Ajax is : to communicate with a Web server asynchronously
without taking away the users ability to interact with the page.
The XMLHttpRequest is what makes this possible. Ajax makes it possible to
update a page without a refresh. By Ajax, we can refresh a particular DOM object
without refreshing the full page. Let's see now what actually happens when a user
submits a request:
1.
2.

3.

Web browser requests for the


content of just the part of the
page that it needs.
Web server analyzes the
received request and builds up
an XML message which is
then sent back to the Web
browser.
After the Web browser
receives the XML message, it
parses the message in order to
update the content of that part
of the page.

How Ajax Works


AJAX uses JavaScript language through HTTP protocol to
send/receive XML messages asynchronously to/from Web server.

Common Steps that AJAX Application Follows


1.
2.
3.
4.
5.

The JavaScript function handEvent() will be invoked when an event occurred


on the HTML element.
In the handEvent() method, an instance of XMLHttpRequest object is
created.
The XMLHttpRequest object organizes an XML message within about the
status of the HTML page, and then sends it to the Web server.
After sending the request, the XMLHttpRequest object listens to the message
from the Web server.
The XMLHttpRequest object parses the message returned from the Web
server and updates it into the DOM object.

Вам также может понравиться