Unit 3
Unit 3
EXtensible M k Language
L
9XML was developed by the SGML Editorial Board of the
W ld Wide
World Wid Web
W b Consortium
C ti (W3C)
What is XML?
9HTML
HTML was designed to display data, with focus on how
data looks
9With
With XML
XML, data can be stored in separate XML files.
files This
way you can concentrate on using HTML for layout and
display, and be sure that changes in the underlying data will
not require any changes to the HTML
9Withh a few
9Wi f lines
li off JavaScript,
J S i you can readd an externall
XML file and update the data content of your HTML
XML Simplifies Data Sharing
XML Tree
<to>Tove</to>
<t >T </t >
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
9And finally the last line defines the end of the root
element: </note>
XML Documents Form a Tree Structure
9The tree starts at the root and branches to the lowest level
of the tree
9All
All elements can have text content and attributes (just like
in HTML)
Example
9The root element in the follwing example is <bookstore>
Comments in XML
9The syntax for writing comments in XML is similar to
that of HTML
<!-- This is a comment -->
White-space is Preserved in XML
9An element
9 l can contain
i other
h elements,
l simple
i l text or a
mixture of both. Elements can also have attributes
XML Naming Rules
9Names cannot start with the letters xml (or XML, or Xml,
etc)
9Imagine
Imagine that the author of the XML document added some
extra information to it:
<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
f J i /f
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
9One
One of the beauties of XML,
XML is that it can often be
extended without breaking applications
XML Attributes
9T k a look
9Take l k at these
h examples:
l
Example1 <person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
p
Example2 <person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
9In the first example gender is an attribute
<?xml version
version="11.00" encoding
encoding="ISO-8859-1"?>
ISO 8859 1 ?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
h di R i d /h di
<body>Don't forget me this weekend!</body>
</note>
9The DOCTYPE declaration in the example above, is a
reference to an external DTD file.
file The content of the file is
shown below
<!DOCTYPE note
[
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT
!ELEMENT from
f (#PCDATA)>
(#PCDATA)
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
Introduction to DTD
9Withh a DTD,
9Wi DTD eachh off your XML files
fil can carry a
description of its own format
PCDATA
9PCDATA means parsed character data
9Tags
Tags inside the text will be treated as markup and entities
will be expanded
9However, parsed character data should not contain any &,
<,, or > characters;; these need to be represented
p byy the
& < and > entities, respectively
CDATA
9CDATA means character data
DTD – Elements
9In a DTD, elements are declared with an ELEMENT
declaration
Declaring Elements
9In a DTD, XML elements are declared with an element
declaration with the following syntax:
<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
Empty Elements
9Empty elements are declared with the category keyword
EMPTY:
<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>
XML example:
p
<br />
Elements with Parsed Character Data
9Elements with only parsed character data are declared
with #PCDATA inside parentheses:
<!ELEMENT element-name (#PCDATA)>
( )
Example:
<!ELEMENT from (#PCDATA)>
9In
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)>
Declaring Only One Occurrence of an Element
<!ELEMENT element-name
element name (child-name)>
(child name)>
Example:
<!ELEMENT note (message)>
( g )
9The * sign
g in the example
p 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
<!ELEMENT element-name (child-name?)>
Example:
l
<!ELEMENT note (message?)>
9The example
9Th l above
b declares
d l that
th t the
th "note"
" t " element
l t can
contain zero or more occurrences of parsed character data,
"to"
to , "from"
from , "header"
header , or "message"
message elements
DTD - Attributes
9In a DTD, attributes are declared with an ATTLIST
declaration
Declaring Attributes
9An attribute declaration has the followingg syntax:
y
<!ATTLIST element-name attribute-name attribute-
type default-value>
DTD example:
l
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" >
The attribute-type can be one of the following:
Type Description
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
The default-value can be one of the following:
Value Explanation
value
l Th default
The d f l valuel off the
h attribute
ib
#REQUIRED The attribute is required
#IMPLIED The attribute is not required
#FIXED value The attribute value is fixed
Example
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788"
f "555 667788" //>
Valid XML:
<contact />
9Use the #IMPLIED keyword if you don't want to force
the author to include an attribute,, and yyou don't have an
option for a default value
#FIXED
Syntax
<!ATTLIST
!ATTLIST element-name
l attribute-name
ib attribute-
ib
type #FIXED "value">
Example
DTD:
<!ATTLIST sender company
p y CDATA #FIXED
"Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company=
company=“Netscape"
Netscape />
9Use 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
Syntax
<!ATTLIST element-name attribute-name (en1|en2|..)
default-value>
Example
DTD:
<!ATTLIST ppayment
y type
yp ((check|cash)
| ) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />
9E i references
9Entity f are references
f to entities
ii
Syntax
<!ENTITY entity-name SYSTEM "URI/URL">
Example
DTD Example:
<!ENTITY writer SYSTEM "wentities.dtd">
<!ENTITY copyright SYSTEM "centities.dtd">
XML example:
<author>&writer;©right;</author>
Introduction to XML Schema
9XML Schema is an XML-based alternative to DTD
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="notetype">
<
<xs:sequence>>
<xs:element name="to" type="xs:string"/>
<xs:element name="from"
name from type
type="xs:string"/>
xs:string />
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="note" type="notetype"/>
</xs:schema>
9 Instead,
I t d these
th applications
li ti have
h built-in
b ilt i ffunctionality
ti lit to t
support the namespace
<?xml version="1.0"?>
<note
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation note.xsd
xsi:noNamespaceSchemaLocation="note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</ t >
</note>
XSD - The <schema> Element
9The <schema> element is the root element of everyy XML
Schema
<?xml version="1.0"?>
<xs:schema>
h
...
...
</xs:schema>
9The <schema> element mayy contain some attributes
It also specifies that the elements and data types that come
from the "http://www.w3.org/2001/XMLSchema"
namespace should be prefixed with xs:
Referencing a Schema in an XML Document
<?xml
? l version="1.0"?>
i "1 0"?
<note
xmlns:xsi="http://www
xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
w3 org/2001/XMLSchema instance"
xsi:noNamespaceSchemaLocation="note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
9The following fragment:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-i
l i "htt // 3 /2001/XMLS h i
nstance"
Indicates that XML document is an instance of XML
schema, and it has come from the namespace
"http://www.w3.org/2001/XMLSchema-instance"
p g
Definingg a Simple
p Element
9The syntax for defining a simple element is:
<xs:element name="aaa" type="bbb"/>
Where aaa is the name of the element and bbb is the
data type of the element
9A ddefault
f l value
l isi automatically
i ll assigned
i d to the
h element
l
when no other value is specified
<xs:complexType name="prodtype">
<xs:attribute
ib name="prodid"
" did" type="xs:positiveInteger"/>
" ii I "/
</xs:complexType>
<xs:element name=
name="product"
product type=
type="prodtype"/>
prodtype />
XSD Complex Types Containing Elements Only
<person>
p
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>
9You can define the "person" element in a schema, like this:
<xs:complexType name="persontype">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
yp g
</xs:sequence>
</xs:complexType>
<xs:element
l name="person"
" " type="persontype"/>
" "/
<xs:complexType name="shoetype">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
/ l T
<xs:element name="shoesize" type="shoetype"/>
XSD Complex Types with Mixed Content
<xs:complexType
< s:comple T pe name
name="lettertype"
"lettert pe" mixed="true">
mi ed "tr e">
<xs:sequence>
<xs:element name
name="name"
name type
type="xs:string"/>
xs:string />
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate"
p type="xs:date"/>
yp
</xs:sequence>
</xs:complexType>
<xs:element name="letter" type="lettertype"/>
9
9Tip: T allow
To ll an element
l to appear an unlimited
li i d number
b
of times, use the maxOccurs="unbounded" statement
Group Indicators
9Group indicators are used to define related sets of
elements
Presenting XML Using XML Processors:
DOM andd SAX
XML Parsers
9 The parser is the engine for interpreting our XML
documents
9 The parser reads the XML and prepares the information
for your application
How to use a parser
1. Create a parser object
2. Pass your XML document to the parser
3 Process the results
3.
Types of Parsers
DOM - Document Object Model
SAX - Simple API for XML
91
XML DOM Introduction
9The XML DOM defines a standard way for accessing and
manipulating
i l ti XML
What is the DOM?
9The
The DOM is a W3C (World Wide Web Consortium)
standard
9The DOM defines a standard for accessing documents like
XML and HTML:
"The W3C Document Object Model (DOM) is a platform
andd language-neutral
l t l interface
i t f th t allows
that ll programs andd
scripts to dynamically access and update the content,
structure and style of a document.
structure, document "
9The DOM is separated into 3 different parts:
9Core DOM - standard model for anyy structured document
9XML DOM - standard model for XML documents
9HTML DOM - standard model for HTML documents
9The DOM defines the objects and properties of all
document elements, and the methods (interface) to access
them
Wh is
What i the
h HTML DOM?
9The HTML DOM defines the objects and properties of
all HTML elements,
elements and the methods (interface) to access
them
Lookk at the
L h following
f ll i XML fragment:
f
<bookstore>
<book category="cooking">
category= cooking >
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
9In the XML above,
above the <title> element is the first child of
the <book> element, and the <price> element is the last
child of the <book>
book element
9Furthermore, the <book> element is the parent node of the
<title>,, <author>,, <year>,
y , and <price>
p elements
9The following
9Th f ll i code d loads
l d "books.xml"
"b k l" into
i xmlDoc
lD usingi
loadXMLDoc() and stores a list of <title> nodes (a node list)
in the variable x:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
g y g ( );
N t The
Note: Th index
i d starts
t t att 0
DOM Node List Length
9The length
g pproperty
p y defines the length
g of a node list ((the
number of nodes)
</head>
<body>
b d
<script type="text/javascript"> Output:
xmlDoc=loadXMLDoc("books.xml");
var k=xmlDoc
k=xmlDoc.getElementsByTagName(
getElementsByTagName("book")[0];
book )[0]; bookstore
document.write(k.parentNode.nodeName+"<br/>"); title
x=xmlDoc.getElementsByTagName("book")[0].childNodes; author
yy=xmlDoc
xmlDoc.getElementsByTagName(
getElementsByTagName("book")[0]
book )[0].firstChild;
firstChild; year
for (i=0;i<x.length;i++) price
{
if (y
(y.nodeType==1)
yp )
{//Process only element nodes (type 1)
document.write(y.nodeName + "<br />");
}
y=y.nextSibling;
}
</script> </body> </html>
XML DOM Node Information
9The nodeName, nodeValue, and nodeType
yp pproperties
p
contain information about nodes
Node Properties
9Three important node properties are:
*nodeName
*nodeValue
*nodeType
Example
Refer slide -112
document.write(xmlDoc.documentElement.nodeName);
The nodeValue Property
9The nodeValue p
property
p y specifies
p the value of a node
Example
Refer slide -105
document write(xmlDoc getElementsByTagName("title")[0]
document.write(xmlDoc.getElementsByTagName( title )[0].childNod
childNod
es[0].nodeValue);
Example
p
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x nodeValue="Easy
x.nodeValue= Easy Cooking
Cooking";;
Result:"Everyday Italian" is replaced with "Easy Cooking"
The nodeType Property
9The nodeType
yp pproperty
p y specifies
p the type
yp of node
9After
After the execution of the code above,
above x.length
x length = is the
number of attributes and x.getNamedItem() can be used to
return an attribute node