UNIT – IV
XML
PREPARED BY,
SAVITHRI.S
DEPARTMENT OF MCA
DSCET
XML
XML stands for eXtensible Markup Language
XML is a markup language much like HTML
XML was designed to store and transport data
XML is a W3C Recommendation
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
But still, the XML above does not DO anything. XML is just information
wrapped in tags.
The Difference Between XML and HTML
XML and HTML were designed with different goals:
XML was designed to carry data - with focus on what data is
HTML was designed to display data - with focus on how
data looks
XML tags are not predefined like HTML tags
SIMPLE PROGRAM
<?xml version="1.0" encoding="UTF-8"?> <author>Per Bothner</author>
<bookstore> <author>Kurt Cagle</author>
<book category="cooking"> <author>James Linn</author>
<title lang="en">Everyday Italian</title> <author>Vaidyanathan Nagarajan</author>
<author>Giada De Laurentiis</author> <year>2003</year>
<year>2005</year> <price>49.99</price>
<price>30.00</price> </book>
</book> <book category="web" cover="paperback">
<book category="children"> <title lang="en">Learning XML</title>
<title lang="en">Harry Potter</title> <author>Erik T. Ray</author>
<author>J K. Rowling</author> <year>2003</year>
<year>2005</year> <price>39.95</price>
<price>29.99</price> </book>
</book> </bookstore>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
The XML Tree Structure
RULES FOR XML
All XML Elements Must Have a Closing Tag
<p>This is a paragraph.</p>
XML Tags are Case Sensitive
Opening and closing tags must be written with the same case:
<message>This is correct</message>
XML Elements Must be Properly Nested
In html : <b><i>This text is bold and italic</b></i>
In xml: <b><i>This text is bold and italic</i></b>
Entity references in XML
There are 5 pre-defined entity references in XML:
< < less than
> > greater than
& & ampersand
' ' apostrophe
" " quotation mark
This will generate an XML error:
<message>salary < 1000</message>
To avoid this error, replace the "<" character with an entity
reference:
<message>salary < 1000</message>
What is an XML Element?
An XML element is everything from (including) the
element's start tag to (including) the element's end tag.
<price>29.99</price>
XML Attributes
XML Attributes Must be Quoted
Attribute values must always be quoted. Either single or double
quotes can be used.
For a person's gender, the <person> element can be written like this:
<person gender="female">
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.
What is XQuery?
XQuery is to XML what SQL is to databases.
XQuery was designed to query XML data.
XQuery Example
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
XQuery FLWOR Expressions
What is FLWOR?
FLWOR (pronounced "flower") is an acronym for "For, Let,
Where, Order by, Return".
For - selects a sequence of nodes
Let - binds a sequence to a variable
Where - filters the nodes
Order by - sorts the nodes
Return - what to return (gets evaluated once for every node)
The following FLWOR expression will select exactly
the same as the path expression above:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
The result will be:
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
THANK YOU