0% found this document useful (0 votes)
27 views12 pages

IPT Chapter 4

The document discusses XSL, XSLT and XPath which are XML-based stylesheet languages. XSLT transforms XML documents into other XML documents or HTML. Templates, elements like for-each, if, choose, apply-templates are used to transform and navigate XML documents.

Uploaded by

beyagetu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

IPT Chapter 4

The document discusses XSL, XSLT and XPath which are XML-based stylesheet languages. XSLT transforms XML documents into other XML documents or HTML. Templates, elements like for-each, if, choose, apply-templates are used to transform and navigate XML documents.

Uploaded by

beyagetu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Integrative programming and technologies

Chapter 4

XSL, XSLT and XPath


4.1 XSL
• XSL stands for EXtensible Stylesheet Language.
• It is an XML-based Stylesheet Language.
• XSL describes how the XML document should be displayed
• XSL consists of three parts:
− XSLT - a language for transforming XML documents
− XPath - a language for navigating in XML documents
− XSL-FO - a language for formatting XML documents
4.2 XSLT
• XSLT stands for XSL Transformations,
• XSLT transforms an XML source-tree into an XML result-tree.
• XSLT transforms an XML document into another XML document that is
recognized by a browser, like HTML and XHTML.
• With XSLT you can add/remove elements and attributes to or from the
output file.
• With XSLT You can also rearrange and sort elements, perform tests and
make decisions about which elements to hide and display, and a lot
more.
• XSLT uses XPath to find information in an XML document.
• XPath is used to navigate through elements and attributes in XML
documents.
• How Does it Work?
− In the transformation process, XSLT uses XPath to define parts of
the source document that should match one or more predefined
templates.
− When a match is found, XSLT will transform the matching part of
the source document into the result document.
• All major browsers such as Internet Explorer ,Chrome, Firefox, Safari and
Opera supports XML, XSLT, and XPath

The XML File

1 ||: By Beya
Integrative programming and technologies

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


<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>

XSLT <xsl:template> Element


• An XSL style sheet consists of one or more set of rules that are called
templates.
• A template contains rules to apply when a specified node is matched.
• The <xsl:template> element is used to build templates.
• The match attribute
− The match attribute is used to associate a template with an XML element.
− The match attribute can also be used to define a template for the entire
XML document.
− The value of the match attribute is an XPath expression (i.e. match="/"
defines the whole document).
Example
<?xml version="1.0" encoding="UTF-8"?>

<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>

2 ||: By Beya
Integrative programming and technologies

<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<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>

Example Explained
− Since an XSL style sheet is an XML document, it always begins with the
XML declaration: <?xml version="1.0" encoding="UTF-8"?>.
− The next element, <xsl:stylesheet>, defines that this document is an XSLT
style sheet document (along with the version number and XSLT
namespace attributes).
− The <xsl:template> element defines a template. The match="/" attribute
associates the template with the root of the XML source document.
− The content inside the <xsl:template> element defines some HTML to write
to the output.
− The last two lines define the end of the template and the end of the style
sheet.

XSLT <xsl:value-of> Element


• The <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:

Note: The select attribute, in the example above, contains an XPath expression.
An XPath expression works like navigating a file system; a forward slash (/)
selects subdirectories.

3 ||: By Beya
Integrative programming and technologies

XSLT <xsl:for-each> and <xsl:sort> Element


• <xsl:for-each> element to loop through the XML elements, and display all
of the records.
• The <xsl:sort> element is used to sort the output.
• To sort the output, simply add an <xsl:sort> element inside the <xsl:for-
each> element in the XSL file:

XSLT <xsl:if> Element


• The <xsl:if> element is used to put a conditional test against the content of
the XML file.
Syntax
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>

• To add a conditional test, add the <xsl:if> element inside the <xsl:for-
each> element in the XSL file instead of <xsl:sort> element in the above
example :
Example
<?xml version="1.0" encoding="UTF-8"?>

<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>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<tr>

4 ||: By Beya
Integrative programming and technologies

<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Note: The value of the required test attribute contains the expression to be
evaluated. The code above will only output the title and artist elements of the
CDs that has a price that is higher than 10.

XSLT <xsl:choose> Element

• The <xsl:choose> element is used in conjunction with <xsl:when> and


<xsl:otherwise> to express multiple conditional tests.

Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

• To insert a multiple conditional test against the XML file, add the
<xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
Example
<?xml version="1.0" encoding="UTF-8"?>

5 ||: By Beya
Integrative programming and technologies

<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>
<xsl:choose>
<xsl:when test="price &gt; 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>

<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

The code above will add a pink background-color to the "Artist" column WHEN
the price of the CD is higher than 10.

6 ||: By Beya
Integrative programming and technologies

XSLT <xsl:apply-templates> Element


• The <xsl:apply-templates> element applies a template to the current element
or it's child nodes.
• If we add a select attribute to the <xsl:apply-templates> element it will
process only the child element that matches the value of the select attribute.
We can use the select attribute to specify the order in which the child
nodes are processed.

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="/">

<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>

<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

<xsl:template match="artist">

7 ||: By Beya
Integrative programming and technologies

Artist: <span style="color:#00ff00">


<xsl:value-of select="."/></span>
<br />
</xsl:template>

</xsl:stylesheet>

A Sample XSL Style Sheet That Has Multiple Matches


In this Example which reads the data in the XML document and displays it in an
HTML table—including the units for various values, as applicable. To extract some
data from ex_01.xml, you need an XSLT style sheet, which is called ex_02.xsl.
(XSLT style sheets usually use the extension .xsl.) This example just strips out
the names of the states and places them into a basic HTML document.

A Sample XML Document (ex_01.xml)


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

<state>
<name>California</name>
<population units="people">33871648</population><!--2000 census--
>
<capital>Sacramento</capital>
<bird>Quail</bird>
<flower>Golden Poppy</flower>
<area units="square miles">155959</area>
</state>

<state>
<name>Massachusetts</name>
<population units="people">6349097</population><!--2000 census-->
<capital>Boston</capital>
<bird>Chickadee</bird>
<flower>Mayflower</flower>
<area units="square miles">7840</area>
</state>

8 ||: By Beya
Integrative programming and technologies

<state>
<name>New York</name>
<population units="people">18976457</population><!--2000 census--
>
<capital>Albany</capital>
<bird>Bluebird</bird>
<flower>Rose</flower>
<area units="square miles">47214</area>
</state>

</states>

XSL Style Sheet (ex_02.xsl)

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


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/states">
<HTML>
<HEAD>
<TITLE>
State Data
</TITLE>
</HEAD>
<BODY>
<H1>
State Data
</H1>
<TABLE BORDER="1">
<TR>
<TD>Name</TD>
<TD>Population</TD>
<TD>Capital</TD>

9 ||: By Beya
Integrative programming and technologies

<TD>Bird</TD>
<TD>Flower</TD>
<TD>Area</TD>
</TR>
<xsl:apply-templates/>
</TABLE>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="state">
<TR>
<TD><xsl:value-of select="name"/></TD>
<TD><xsl:apply-templates select="population"/></TD>
<TD><xsl:apply-templates select="capital"/></TD>
<TD><xsl:apply-templates select="bird"/></TD>
<TD><xsl:apply-templates select="flower"/></TD>
<TD><xsl:apply-templates select="area"/></TD>
</TR>
</xsl:template>

<xsl:template match="population">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@units"/>
</xsl:template>

<xsl:template match="capital">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="bird">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="flower">

10 ||: By Beya
Integrative programming and technologies

<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="area">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@units"/>
</xsl:template>

</xsl:stylesheet>

Note the expression "." here. You use "." with the select attribute to specify the
current node. You can handle attributes very much like you handle elements. All
that's different is that you have to preface the attribute name with @. For example,
say that you want to recover the value of the units attributes in the <population>
and <area> elements of the XML example. To get the values of the units
attribute, you simply need to refer to it as @units.

Here's the HTML you get, including the HTML table:

<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
State Data
</TITLE>
</HEAD>

<BODY>
<H1>
State Data
</H1>
<TABLE BORDER="1">
<TR>
<TD>Name</TD>
<TD>Population</TD>
<TD>Capital</TD>

11 ||: By Beya
Integrative programming and technologies

<TD>Bird</TD>
<TD>Flower</TD>
<TD>Area</TD>
</TR>

<TR>
<TD>California</TD>
<TD>33871648 people</TD>
<TD>Sacramento</TD>
<TD>Quail</TD>
<TD>Golden Poppy</TD>
<TD>155959 square miles</TD>
</TR>

<TR>
<TD>Massachusetts</TD>
<TD>6349097 people</TD>
<TD>Boston</TD>
<TD>Chickadee</TD>
<TD>Mayflower</TD>
<TD>7840 square miles</TD>
</TR>

<TR>
<TD>New York</TD>
<TD>18976457 people</TD>
<TD>Albany</TD>
<TD>Bluebird</TD>
<TD>Rose</TD>
<TD>47214 square miles</TD>
</TR>
</TABLE>
</BODY>
</HTML>

12 ||: By Beya

You might also like