IT702 – AWT (Jul – Dec 2016) 31-08-2016

Querying and manipulating XML documents

Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 30-Aug-16

Raw XML Display

 Plain XML documents are generally displayed literally by browsers.

 Most browsers do the following -
 Chrome, Firefox etc. note that there is no style information included.

 They include a default style to handle XML docs, which is just a display
of the XML tree structure of the document. (called the raw display.)

 May be used to check document structure and content for testing and
verification purposes.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 1
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Need for Processing instructions (PI)
 A PI is a command to the program processing the XML document to
handle it in a certain way.
 may occur anywhere in the XML document (but usually are written
first).

 XML documents are typically processed by more than one program.
 Programs that do not recognize a given PI will just ignore it.

 General format of a PI: <?target instructions?>

E.g. <?xml-stylesheet type="text/css" href="mystyle.css“ ?>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 2
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Displaying XML Documents with CSS

 CSS is the same for XML as it is for HTML, but –
 No current browser does a good job of supporting CSS, particularly
when used with XML.

 Therefore -
 You should make sure everyone who views your pages uses the same
version of the same browser (this is possible in some companies),
or
 You should test your pages in all the most common browsers.

Dept of IT, NITK Surathkal 30-Aug-16

Displaying XML Documents with CSS (contd.)

 Things to do when using CSS for XML –
 Designer has to decide the way text is to arranged on the result page
beforehand.

 All display characteristics of each element must be explicitly defined.
 A style must be defined for every XML tag, or the browser displays it in a
default mode.

 Elements are displayed in the order in which they are encountered in
the XML document.
 No reordering of elements or other processing is possible.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 3
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Displaying XML Documents with CSS (contd.)

 An xml-stylesheet processing instruction can be used to associate a
general XML document with a style sheet.
 To be included in the XML file after schema declarations etc…

Syntax:

<?xml-stylesheet type=“text/css”
href=“empstyle.css” ?>

Dept of IT, NITK Surathkal 30-Aug-16

Displaying XML Documents with CSS (contd.)
The display property
 Every XML element that is formatted by a CSS command is considered to be
in an invisible “box”
 The box can have one of three display properties

 display: block
 The element will start on a new line, and so will the element that follows
it (HTML’s paragraph is an example)
 display: inline
 (default) The element will not start on a new line, or cause the next
element to start on a new line (HTML’s bold is an example)
 display: none
 The element is hidden and will not appear on the display

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 4
IT702 – AWT (Jul – Dec 2016) 31-08-2016

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml
<?xml-stylesheet type="text/css" href="empstyle.css"?>
<employees>
<person>
<name>
<fname>Emp</fname>
<lname>One</lname>
<mname>A</mname>
</name>
<parent rel="father">Empone father</parent>
<parent rel="mother">Empone mother</parent>
<spouse>Empone spouse</spouse>
<company>XYZ Technologies</company>
<age>40 years</age>
<gender>male</gender>
<address> empdata.xml
<street>Some Street</street>
<city>Delhi</city>
<country>India</country>
<pincode>111001</pincode>
</address>
<dept>Finance</dept>
<role>Accountant</role>
</person>
. . .
</employees>

CSS file
(for displaying emp name, role and company only)

employees{background-color:#ffffee; width: 100%; }

person {display:block;
{ margin-bottom:30pt; margin-left:0; }

name {display:block;
{ color: magenta; font-size: 20pt; }

role {display:block;
{ color: blue; font-size: 14pt; }

dept, company {color: cyan; font-size:
font 20pt; }

parent, spouse, age, gender, address {display:none;}
{

empstyle.css for
empdata.xml
Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 5
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Formatted Display in browser window

CSS is quite effective but not
the best as style rules are
applied to all occurrences of a
particular tag in the XML doc.

Dept of IT, NITK Surathkal 30-Aug-16

Transforming XML Data for Display
 Standard XML querying/transformation languages -
 XQuery
 An XML query language with a rich set of features to specify
clauses, sub-queries etc.
 XSLT
 A simple XML based language designed for transformation from
XML to XML and XML to (X)HTML

 Both use XPath to navigate the XML document tree to query/reach a
specific node.
* XPath - Simple language consisting of path expressions that are used
to traverse the XML document tree.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 6
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XPath

 XPath is used to address (select) parts of documents using
path expressions

 A path expression is a sequence of steps separated by “/”
 Similar to file names in a directory hierarchy

 Result of path expression:
 set of values that along with their containing elements/attributes
match the specified path.

Dept of IT, NITK Surathkal 30-Aug-16

XPath (Cont.)
 The initial “/” denotes root of the document (above the top-level tag)

 Path expressions are evaluated left to right

 Selection conditions may follow any step in a path, enclosed in square brackets
 E.g. /employees/person [age>40]
 E.g. /employees/person [spouse]
 returns all <person> elements containing a <spouse> sub-element
 Attributes are accessed using “@”
 E.g. /employees/person [parent]/@rel=“mother”
 returns the <person> elements where <parent> stores mother’s name.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 7
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XSLT (XML Style Sheet Transformations)
 Used to transform XML documents into different forms or formats.
 E.g. create a new XML document with part of the content or format
the content for printing.
 Common (easiest!) use of XSLT is to transform XML documents
into HTML, primarily for display.
 XSLT is XML based, so can be validated.
 Based on eXtensible Stylesheet Language XSL (3 parts)
 XPath – a language for navigating XML docs
 XSLT – a language for transforming XML docs
 XSL-FO – a language for formatting XML docs (e.g. for display on
screen, for printing or for other media)

Dept of IT, NITK Surathkal 30-Aug-16

XSLT Processing for Display
 XSLT processors take the XML and its XSLT doc as input .
 Parts of the XML doc are selected and possibly modified based on the
display requirements to produce a new doc.

 The output doc can be stored for future use by applications, or it may
be immediately displayed on a browser. (Ultimately, both XML and
XSLT documents remain the same.)

Analogy: XML ⇄ XSLT
(like a database) (like a SQL query that
creates a view.)

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 8
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XSLT Processing for Display (contd.)

Formatted
Output
XML Doc

XML Parser
Formatted
Source Tree Output
Trans--
Trans Formatting
formation
Result Tree Formatted
Output
XSLT
Stylesheet

Dept of IT, NITK Surathkal 30-Aug-16

Serving XML to Web Users
 Basic requirements:
 A “correct and valid” XML doc and a web server

 Additional requirements for simple but often inefficient method :
 A CSS Stylesheet.

 Additional requirements for complex but powerful method:
 XSLT stylesheets.
 An XML parser.
 A server side program to handle the process and join the
pieces.
 A CSS stylesheet (optional) to control how data looks in a
browser.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 9
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Serving XML to Web Users (contd.)

XML XSLT
Doc Stylesheet

XML Processor XHTML
(xsltproc) representation

Cascading
Stylesheet
(CSS)
Server Side Program

Web Server

Dept of IT, NITK Surathkal 30-Aug-16

Using XSLT for presentation
 XSLT document contains one or more set of commands called
templates
 Each template specifies rules to apply when a node matches a pattern.
 XPath expression is used to specify the location of elements to
which the templates should apply.
 The XSLT processor will look for parts of the input document that
match a template and apply its commands when a match is found.
 Two models -
 Template-driven works with highly regular data (e.g. XML docs)
 Data-driven works with more loosely structured data with a
recursive structure (like XHTML documents)

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 10
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XSLT Structure
 STEP 1: Create the XSL stylesheet (.xsl extension)
<?xml version =“1.0” …?>
<xsl:stylesheet
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
. . .
</xsl:stylesheet>

 STEP 2: Link the XML and XSL files
<?xml version =“1.0” …?>
<xsi:schema ... . . >
<xml:stylesheet type=“text/xsl” href=“empstyle.xsl”?>
<employees>
<person> .. </person>
. . . .
</employees>

Dept of IT, NITK Surathkal 30-Aug-16

XSLT Structure
<xsl:template> element
 Used to build templates.
 The match attribute is used to associate a template with an XML
element.

Syntax: <xsl:template match = “/”>

 “match” attribute’s value is an XPath expression (i.e. match="/"
defines the whole document).

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 11
IT702 – AWT (Jul – Dec 2016) 31-08-2016

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml
<?xml-stylesheet type="text/css" href="empstyle.xsl"?>
<employees>
<person>
<name>
<fname>Emp</fname>
<lname>One</lname>
<mname>A</mname>
</name>
<parent rel="father">Empone father</parent>
<parent rel="mother">Empone mother</parent>
<spouse>Empone spouse</spouse>
<company>XYZ Technologies</company>
<age>40 years</age>
<gender>male</gender>
<address> empdata.xml
<street>Some Street</street>
<city>Delhi</city>
<country>India</country>
<pincode>12323</pincode>
</address>
<dept>Finance</dept>
<role>Accountant</role>
</person>
.. .
/employees>
Dept of IT, NITK Surathkal 30-Aug-16

Using the <xsl:template> element

<?xml version="1.0” …. ?>
<xsl:stylesheet
<
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/"> empstyle.xsl
.
.
.
.
</xsl:template>

</
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 12
IT702 – AWT (Jul – Dec 2016) 31-08-2016

XML to HTML transformation
<?xml version="1.0” …. ?>
<xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform">

empstyle.xsl
<xsl:template match="/">
<html>
<body>
<h2 align=“center”>Employee Details</h2>
<p><b>Name ------------- Department</b> </p>
<p> . ------------- . </p>
</body>
</html>

</xsl:template>

</xsl:stylesheet> Output

Dept of IT, NITK Surathkal 30-Aug-16

XSLT Structure
<xsl:value-of> Element
 can be used to extract the value of an XML element and add it
to the output stream of the transformation.
Syntax:

<xsl:value-of select=“path_to_node_to_be_accessed"/>

 select attribute - contains an XPath expression (/).
 An XPath expression works like navigating a file system; a
forward slash (/) selects subdirectories.
e.g. “employees/person/age” to access the node storing age
info.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 13
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:value-of> element
<?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>Employee Details</h2>
<p><b>Name ------------- Department</b></p>
<p>
<xsl:value-of select="employees/person/name"/> --- belongs to ---
<xsl:value-of select="employees/person/dept"/>
</p>
</body>
</html>
</
</xsl:template>
</
</xsl:stylesheet>

Here result is a little disappointing -
only one line of data was copied from
the XML document to the output.

XSLT Structure
<xsl:for-each> Element
 can be used to select every XML element of a specified node-set.
<xsl:for-each select=“path_to_node’s_parent">
<xsl:value-of select=“path_to_node"/>

. . .
</xsl:for-each>

 The output can be filtered from the XML file by adding a criterion to the
select attribute in the <xsl:for-each> element.
Legal filter operators are: = , != , <, >, >= and <=
E.g. <xsl:for-each select="employees/person[age>25]”>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 14
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:for-each> element
<?xml version="1.0" encoding="ISO-8859-1"?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
<xsl:template match="/">
<html>
<body>
<h2>Employee Details</h2>
<p><b> Name ----------------- Department</b></p>
<p>
<xsl:for-each select="employees/person">
<xsl:value-of select=“name"/> -----------------
<xsl:value-of select="dept"/>
<br />
</xsl:for-each>
</p>
</body>
</html>
</
</xsl:template>
</
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

XSLT Structure
<xsl:sort> Element
 used to sort the output.
<xsl:for-each select=“path_to_node’s_parent">

<xsl:sort select=“node_to_be_sorted"/>
<xsl:value-of select=“path_to_node"/>
</xsl:for-each>

 To sort the output, simply add an <xsl:sort> element inside the
<xsl:for-each> element in the XSL file:
 select attribute of <xsl:sort> indicates what XML element to
sort on.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 15
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:sort> Element
<?xml version="1.0" encoding="ISO-8859-1"?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
<xsl:template match="/">
<html>
<body>
<h2>Employee Details</h2>
<p><b>Name ------------- Department</b></p>
<p>
<xsl:for-each select="employees/person">
<xsl:sort select="age"/>
<xsl:value-of select=“name"/> --------------
<xsl:value-of select=“dept"/>
</xsl:for-each>
</p>
</body>
</html>
</
</xsl:template>
</
</xsl:stylesheet>

Dept of IT, NITK Surathkal

XSLT Structure
<xsl:if> Element
 used to put a conditional test against the content of the XML file.
 To add a conditional test, add the <xsl:if> element inside the
<xsl:for-each> element

<xsl:for-each select = “path_to_parent_node">
<xsl:if test=“condition">
<xsl:value-of select = “path_to_node"/>
</xsl:if>
</xsl:for-each>

 The value of the required test attribute contains the expression to be
evaluated.
Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 16
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:if> Element
<?xml version="1.0" encoding="ISO-8859-1"?
<
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<
<html> <body>
<h2>Employee Details</h2>
<p align="center"><b>Name ------------- Department</b></p>
<p align="center">
<xsl:for-each select="employees/person">
<xsl:if test=“age>35”>
<xsl:value-of select=“name"/> --- by ---
<xsl:value-of select=“dept"/>
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>
</
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

XSLT Structure
<xsl:choose> Element
 is used in conjunction with <xsl:when> and <xsl:otherwise> to
express multiple conditional tests.
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

 The value of the required test attribute contains the expression to be
evaluated.
Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 17
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:choose> Element
.
.
<
<xsl:template match="/">
<html> <body>
<h2>Employee Details</h2>
<p align="center"><b>Name ------------- Department</b></p>
<xsl:for-each select="employees/person">
<p>
<xsl:choose>
<xsl:when test=“gender = female">
<font color=“pink”><xsl:value-of select=“name"/></font>
</xsl:when>
<xsl:otherwise>
<font color=“blue”><xsl:value-of select=“name"/></font>
</xsl:otherwise>
</xsl:choose>
------------- <xsl:value-of select=“dept"/>
<br />
</p>
</xsl:for-each>

... .
</
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

Another Example
 Using XSLT, the display of an XML document can be formatted to fit
into HTML tables, lists etc.

 Following example shows usage of XSLT capabilities for transforming
XML data so as to display content selected in a tabular form.

 Similarly you can try and use lists, nested lists, nested tables etc…

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 18
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:template> element
<?xml version="1.0“ …. ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
< empstyle.xsl
<html>
<body>
<h2>Employee Details</h2>
<table border="1">
<tr bgcolor=“cyan">
<th>NAME</th>
<th>DEPARTMENT/th>
</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template> Output
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

Using the <xsl:value-of> element
<?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>Employee Details</h2>
<table border="1">
<tr bgcolor="cyan">
<th>NAME</th>
<th>DEPARTMENT</th>
</tr>
<tr>
<td> <xsl:value-of select="employees/person/name"/> </td>
<td> <xsl:value-of select="employees/person/department"/></td>
</tr>
</table> Only one line of data was copied from
</body> the XML document to the output. So
</html>
</xsl:template> use <xsl:for-each> element to select
</xsl:stylesheet> from all records.

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 19
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:for-each> element
<?xml version="1.0" encoding="ISO-8859-1"?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
<xsl:template match="/">
<html>
<body>
<h2>Employee Details</h2>
<table border="1">
<tr bgcolor="cyan">
<th>NAME</th>
<th>DEPARTMENT</th>
</tr>
<xsl:for-each select="employees/person">
<tr>
<td> <xsl:value-of select=“name"/> </td>
<td> <xsl:value-of select=“department"/> </td>
</tr>
</xsl:for-each>
</table></body></html>
</xsl:template>
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

Using the <xsl:sort> Element
. . .
<xsl:template match="/">
<
<html> <body>
<h2>Employee Details</h2>
<table border="1">
<tr bgcolor="cyan">
<th>NAME</th>
<th>DEPARTMENT</th>
</tr>
<xsl:for-each select="employees/person">
<xsl:sort select=“age"/>
<tr>
<td> <xsl:value-of select=“name"/> </td>
<td> <xsl:value-of select=“dept"/> </td>
</tr>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
</
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 20
IT702 – AWT (Jul – Dec 2016) 31-08-2016

Using the <xsl:if> Element
. . .
<xsl:template match="/">
<
<html> <body>
<h2>Employee Details</h2>
<table border="1">
<tr bgcolor="cyan">
<th>NAME</th>
<th>DEPARTMENT</th>
</tr>
<xsl:for-each select="employees/person">
<xsl:if test=“age>35” >
<tr>
<td> <xsl:value-of select=“name"/> </td>
<td> <xsl:value-of select=“dept"/> </td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</
</xsl:stylesheet>

Dept of IT, NITK Surathkal 30-Aug-16

Using the <xsl:choose> Element
<table border="1">
<tr bgcolor="cyan">
<th>NAME</th>
<th>DEPARTMENT</th>
</tr>
<xsl:for-each select="employees/person">
<tr>
<xsl:choose>
<xsl:when test=“gender = ‘female’ ”>
<td bgcolor=“magenta”><xsl:value-of select=“name"/></td>
<td> <xsl:value-of select="dept"/> </td>
</xsl:when>
<xsl:otherwise>
<td bgcolor=“blue”> <xsl:value-of select=“name"/> </td>
<td> <xsl:value-of select="dept"/> </td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
. . .

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 21
IT702 – AWT (Jul – Dec 2016) 31-08-2016

References

 XML in a Nutshell, 3rd edition
Elliotte Rusty Harold,W. Scott Means, Publisher: O'Reilly; 3 edition
(September, 2004)

 XSLT : Programmer's Reference (Programmer to
Programmer)
Michael Kay, Publisher: Wrox; 2 edition (May 3, 2001)

Dept of IT, NITK Surathkal 30-Aug-16

© Dr. Sowmya Kamath S, Dept of IT, NITK Surathkal 22