CH5 WEB Lecture
CH5 WEB Lecture
Chapter 55
1
XSL
• XSL = eXtensible Stylesheet Language
• XSL consists of
– XPath (navigation in documents)
– XSLT (T for transformations)
– XSLFO (FO for formatting objects)
• This is a rather complex language for typesetting
(i.e., preparing text for printing)
• It will not be taught
2
<?xml
<?xml version="1.0"
version="1.0" encoding="ISO-8859-1"?>
encoding="ISO-8859-1"?>
<catalog>
<catalog>
<cd
<cd country="UK">
country="UK">
<title>Dark
<title>Dark Side
Side of
of the
the Moon</title>
Moon</title>
<artist>Pink
<artist>Pink Floyd</artist>
Floyd</artist>
<price>10.90</price>
<price>10.90</price>
</cd>
</cd> An XML document
<cd
<cd country="UK">
country="UK">
<title>Space
<title>Space Oddity</title>
Oddity</title>
<artist>David
<artist>David Bowie</artist>
Bowie</artist>
<price>9.90</price>
<price>9.90</price>
</cd>
</cd>
<cd
<cd country="USA">
country="USA">
<title>Aretha:
<title>Aretha: Lady
Lady Soul</title>
Soul</title>
<artist>Aretha
<artist>Aretha Franklin</artist>
Franklin</artist>
<price>9.90</price>
<price>9.90</price>
</cd>
</cd> 3
</catalog>
</catalog>
XSLT Stylesheet
• An XSLT stylesheet is a program that
transforms an XML document into HTML
document or any formatted output.
• For example:
– Transforming XML to XHTML (HTML that
conforms to XML syntax)
– Transforming an XML document to a
formatted XM to display.
4
A Few Things About XSL
5
Applying XSLT Stylesheets to
XML Documents
• There are three ways of applying an
XSLT stylesheet to an XML document
– Directly applying an XSLT processor to the
XML document and the XSLT stylesheet
– Calling an XSLT processor from within a
(Java) program
– Adding to the XML document a link to the
XSL stylesheet and letting the browser do
the transformation
6
Using an XSL Processor
XSL
XSL
stylesheet
stylesheet
Result
Resultisis
either
eitheran
an
XML
XML
XSL Processor XML,
XML,HTML
HTML
document
document or
ortext
text
document
document
7
Letting a Browser Perform the
Transformation
<?xml
<?xml version="1.0"
version="1.0" encoding="ISO-8859-1"?>
encoding="ISO-8859-1"?>
<?xml-stylesheet
<?xml-stylesheet type="text/xsl"
type="text/xsl"
href=“catalog.xsl"?>
href=“catalog.xsl"?>
<catalog>
<catalog>
<cd
<cd country="UK">
country="UK">
<title>Dark
<title>Dark Side
Side of
of the
the Moon</title>
Moon</title>
<artist>Pink
<artist>Pink Floyd</artist>
Floyd</artist>
<price>10.90</price>
<price>10.90</price>
</cd>
</cd>
…… A link to the stylesheet
</catalog>
</catalog>
8
The Root of the XSL Document
• The Root of the XSL document should be
one of the following lines:
<xsl:stylesheet version="1.0"
>"xmlns:xsl="http://www.w3.org/1999/XSL/Transform
<xsl:transform version="1.0"
>"xmlns:xsl="http://www.w3.org/1999/XSL/Transform
<xsl:template
<xsl:template match="/">
match="/">
<html>
<html>
<body>
<body>
<h1>Hello
<h1>Hello World</h1>
World</h1>
</body>
</body>
</html>
</html>
</xsl:template>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>
12
>html<
>body<
>h1>Hello World</h1<
>body/<
>html/<
<xsl:template
<xsl:template match="/">
match="/">
<html>
<html>
<body>
<body>
<xsl:apply-templates
<xsl:apply-templates select="catalog/cd"/>
select="catalog/cd"/>
</body>
</body>
</html> >html<
</html>
</xsl:template>
</xsl:template> >body<
<xsl:template
<xsl:template match="cd">
match="cd"> >h2>A CD!</h2<
<h2>A
<h2>A CD!</h2>
CD!</h2> >h2>A CD!</h2<
</xsl:template>
</xsl:template> >h2>A CD!</h2<
</xsl:stylesheet> >body/<
</xsl:stylesheet>
>html/< 15
The Most Frequently Used
Elements of XSL
• <xsl:value-of select=“xpath-expression”/>
– This element extracts the value of a node from the
nodelist located by xpath-expression
• <xsl:for-each select=“xpath-expression”/>
– This element loops over all the nodes in the nodelist
located by xpath-expression
• <xsl:if test=“xpath-expression”/>,
<xsl:if test=“xpath-expression=value”/>, etc.
– This element is for conditional processing
16
The <xsl:value-of> Element
<xsl:value-of select=“xpath-expression”/>
• The XSL element <xsl:value-of> can be
used to extract the value of an element
that is selected from the source XML
document
• The extracted value is added to the
output stream
• The selected element is located by an
XPath expression that appears as the
value of the select attribute
17
Selected values
18
<?xml
<?xml version="1.0"
version="1.0" encoding="ISO-8859-1"?>
encoding="ISO-8859-1"?>
<xsl:stylesheet
<xsl:stylesheet version="1.0"
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/
xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
Transform">
<xsl:template
<xsl:template match="/">
match="/">
<html>
<html>
<body>
<body>
<h2>A
<h2>A CD
CD Catalog</h2>
Catalog</h2>
<table
<table border="1">
border="1">
<tr
<tr bgcolor=“yellow">
bgcolor=“yellow">
<th>Title</th>
<th>Title</th>
<th>Artist</th>
<th>Artist</th>
</tr>
</tr>
19
<tr>
<tr>
<td><xsl:value-of
<td><xsl:value-of
select="catalog/cd/title"/>
select="catalog/cd/title"/>
</td>
</td>
<td><xsl:value-of
<td><xsl:value-of
select="catalog/cd/artist"/>
select="catalog/cd/artist"/>
</td>
</td>
</tr>
</tr>
</table>
</table>
</body>
Note that only the first matched
</body>
</html> element is retrieved for each
</html>
</xsl:template> <xsl:value of>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>
20
The <xsl:for-each> Element
xsl:for-each select=“xpath-<
>/”expression
• The <xsl:for-each> element loops over all
the nodes in the nodelist of the XPath
expression that appears as the value of
the select attribute
• The value of each node can be extracted
by an <xsl:value-of> element
21
All the values are selected
22
<?xml
<?xml version="1.0"
version="1.0" encoding="ISO-8859-1"?>
encoding="ISO-8859-1"?>
<xsl:stylesheet
<xsl:stylesheet version="1.0"
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/
xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
Transform">
<xsl:template
<xsl:template match="/">
match="/">
<html>
<html>
<body>
<body>
<h2>A
<h2>A CD
CD Catalog</h2>
Catalog</h2>
<table
<table border="1">
border="1">
<tr
<tr bgcolor=“yellow">
bgcolor=“yellow">
<th>Title</th>
<th>Title</th>
<th>Artist</th>
<th>Artist</th> As in the
</tr>
</tr> previous
example23
<xsl:for-each
<xsl:for-each select="catalog/cd">
select="catalog/cd">
<tr>
<tr>
<td><xsl:value-of
<td><xsl:value-of select="title"/>
select="title"/>
</td>
</td>
<td><xsl:value-of
<td><xsl:value-of select="artist"/>
select="artist"/>
</td>
</td>
</tr>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</table>
</body>
</body>
</html>
</html>
</xsl:template>
</xsl:template> Note that all the /catalog/cd
</xsl:stylesheet>
elements are retrieved
</xsl:stylesheet>
24
Consider the following change in the select attribute:
<xsl:for-each
<xsl:for-each
select="catalog/cd[price<10]">
select="catalog/cd[price<10]">
<tr>
<tr>
<td><xsl:value-of
<td><xsl:value-of select="title"/>
select="title"/>
</td>
</td>
<td><xsl:value-of
<td><xsl:value-of select="artist"/>
select="artist"/>
</td>
</td>
</tr>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</table>
</body>
</body>
</html>
</html>
</xsl:template>
</xsl:template> Only elements that satisfy
/catalog/cd[price<10]
</xsl:stylesheet>
</xsl:stylesheet> are retrieved
25
The <xsl:if> Element
• The <xsl:if> element is used for
conditional processing
• The condition appears as the value of the
test attribute, for example:
<xsl:if test="price > 10">
some output ...
</xsl:if>
• The elements inside the <xsl:if>
element are processed if the condition is
true
26
Note
• Processing the inside elements means
– Copying them into the output stream if they
are not XSL elements, and
– Evaluating them if they are XSL elements
• If the value of the test attribute is just an
XPath expression (i.e., without any
comparison), then the test is satisfied if
the nodelist of this XPath expression is
not empty
27
<?xml
<?xml version="1.0"
version="1.0" encoding="ISO-8859-1"?>
encoding="ISO-8859-1"?>
<xsl:stylesheet
<xsl:stylesheet version="1.0"
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/
xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
Transform">
<xsl:template
<xsl:template match="/">
match="/">
<html>
<html>
<body>
<body>
<h2>A
<h2>A CD
CD Catalog</h2>
Catalog</h2>
<table
<table border="1">
border="1">
<tr
<tr bgcolor=“yellow">
bgcolor=“yellow">
<th>Title</th>
<th>Title</th>
<th>Artist</th>
<th>Artist</th> As in the
</tr>
</tr> previous
examples28
<xsl:for-each
<xsl:for-each select="catalog/cd">
select="catalog/cd">
<xsl:if
<xsl:if test="price
test="price >
> 10">
10">
<tr>
<tr>
<td><xsl:value-of
<td><xsl:value-of select="title"/>
select="title"/>
</td>
</td>
<td><xsl:value-of
<td><xsl:value-of select="artist"/>
select="artist"/>
</td>
</td>
</tr>
</tr>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</table>
</body>
</body>
</html>
</html> Only /catalog/cd with
</xsl:template>
</xsl:template> price>10 are retrieved
</xsl:stylesheet>
</xsl:stylesheet> 29
Some Other XSL Elements
• The <xsl:choose> element allows to choose multiple
conditions
• The <xsl:text> element allows to insert free text in the
output
• The <xsl:copy-of> element creates a copy of the current
node
• The <xsl:sort> element used to sort element any node
• The <xsl:output> element creates outputs to any format
• The <xsl:variable> element creates a variable for
temporary buffering.
• The <xsl:comment> element is used to create a
comment node in the result tree
• The <xsl:number> element is used to count number of
row.
30
The <xsl:choose> Element
• The <xsl:choose> element is used in
conjunction with <xsl:when> and
<xsl:otherwise> to express test with
multiple conditions
• There can be many <xsl:when> inside
an <xsl:choose> element, but there
should be a single <xsl:otherwise>
inside an <xsl:choose> element
31
Using <xsl:choose>
• To insert a conditional choose against the
content of the XML file, simply add the
<xsl:choose>, <xsl:when>, and
<xsl:otherwise> elements to your XSL
document like this:
<xsl:choose>
<xsl:when test="price > 10">
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ....
</xsl:otherwise>
</xsl:choose> 32
<xsl:for-each
<xsl:for-each select="catalog/cd"><tr>
select="catalog/cd"><tr>
<td><xsl:value-of
<td><xsl:value-of select="title"/></td>
select="title"/></td>
<xsl:choose>
<xsl:choose>
<xsl:when
<xsl:when test="price
test="price >
> 10">
10">
<td
<td bgcolor="red">
bgcolor="red">
<xsl:value-of
<xsl:value-of select="artist"/></td>
select="artist"/></td>
</xsl:when>
</xsl:when>
<xsl:when
<xsl:when
test="price>9
test="price>9 and
and price<=10">
price<=10">
<td
<td bgcolor="gray">
bgcolor="gray">
<xsl:value-of
<xsl:value-of select="artist"/></td>
select="artist"/></td>
</xsl:when>
</xsl:when>
<xsl:otherwise>
<xsl:otherwise>
<td><xsl:value-of
<td><xsl:value-of select="artist"/></td>
select="artist"/></td>
</xsl:otherwise>
</xsl:otherwise>
</xsl:choose></tr>
</xsl:choose></tr>
</xsl:for-each>
</xsl:for-each> 33
<xsl:text>
<?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>My CD Collection</h2>
<p>Titles:
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:if test="position() < last()-1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-1">
<xsl:text>, and </xsl:text>
</xsl:if>
34
<xsl:text> (cont’d)
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
35
<xsl:copy-of>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="header">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<xsl:copy-of select="$header"/>
<xsl:for-each select="catalog/cd">
36
<xsl:copy-of> (cont’d)
<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>
37
The <xsl:sort> Element
38
Example of <xsl:sort> Element
39
<xsl:for-each
<xsl:for-eachselect="catalog/cd">
select="catalog/cd">
<xsl:sort
<xsl:sortselect="artist"/>
select="artist"/>
<tr>
<tr>
<td><xsl:value-of
<td><xsl:value-ofselect="title"/>
select="title"/>
</td>
</td>
<td><xsl:value-of
<td><xsl:value-ofselect="artist"/>
select="artist"/>
</td>
</td>
</tr>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</table>
</body>
</body>
</html>
</html>
</xsl:template>
</xsl:template> The /catalog/cd elements
are sorted according to the
</xsl:stylesheet>
</xsl:stylesheet> value of the artist element
40
The <xsl:if> Element
• The <xsl:if> element is used for conditional processing
<?xml
<?xmlversion="1.0"
version="1.0"encoding="ISO-8859-1"?>
encoding="ISO-8859-1"?>
<xsl:stylesheet
<xsl:stylesheetversion="1.0"
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/
xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
Transform">
<xsl:template
<xsl:templatematch="/">
match="/">
<html>
<html>
<body>
<body>
<h2>A
<h2>ACDCDCatalog</h2>
Catalog</h2>
<table
<tableborder="1">
border="1">
<tr
<trbgcolor=“yellow">
bgcolor=“yellow"> As in the
<th>Title</th>
<th>Title</th>
<th>Artist</th>
<th>Artist</th> previous
</tr>
</tr> examples
42
<xsl:for-each
<xsl:for-eachselect="catalog/cd">
select="catalog/cd">
<xsl:if
<xsl:iftest="price
test="price>>10">
10">
<tr>
<tr>
<td><xsl:value-of
<td><xsl:value-ofselect="title"/>
select="title"/>
</td>
</td>
<td><xsl:value-of
<td><xsl:value-ofselect="artist"/>
select="artist"/>
</td>
</td>
</tr>
</tr>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</table>
</body>
</body>
</html>
</html>
</xsl:template>
</xsl:template> Only /catalog/cd with
price>10 are retrieved
</xsl:stylesheet>
</xsl:stylesheet> 43
44
XSLT Output
Sample XML Sample XSLT
<person> <xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<name first="Neil"
last="Armstrong" /> <xsl:output method="text" />
<xsl:template match="/person">
<quote>...one giant leap for
mankind.</quote> <xsl:value-of select="concat(
</person> name/@first, ' ',
name/@last)" />
said "
<xsl:value-of
Sample Output select="quote" />"
Neil Armstrong said "...one giant </xsl:template>
leap for mankind." </xsl:stylesheet>
45
XSLT Output
Sample XML Sample XSLT HTML
<zoo> <xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform ">
<birds>
<xsl:output method="html" />
<albatross pop="4" />
<xsl:template match="/zoo">
<buzzard pop="2" />
<html><head><title>Zoo</title>
<chickadee pop="12" /> </head><body>
</birds> <xsl:for-each select="*">
<mammals>
<h1>
<aardvark pop="5" />
<xsl:value-of select="name(.)" />
<bat pop="200" />
<cheetah pop="2" /> </h1>
</mammals> </xsl:for-each>
</zoo> </body></html>
</xsl:template>
</xsl:stylesheet>
46
XSLT Output
Sample XML Result HTML
<zoo> <html><head><title>Zoo</title>
<birds> </head><body>
<albatross pop="4" /> <h1>
<buzzard pop="2" /> birds
<chickadee pop="12" /> </h1>
</birds> <h1>
<mammals> mammals
<aardvark pop="5" /> </h1>
<bat pop="200" /> </body></html>
<cheetah pop="2" />
</mammals>
</zoo>
47
XSLT Variables: variable
XML XSLT fragment
<zoo> <xsl:template match="birds | mammals">
<birds> <xsl:variable name="total-animals"
<albatross pop="4" /> select="sum(*/@pop)" />
<buzzard pop="2" /> <ul><xsl:for-each select="*">
<chickadee pop="12" /> <li><xsl:value-of
</birds>
<mammals>
select="name(.)" />
<aardvark pop="5" /> (<xsl:value-of select="round(100 *
<bat pop="200" /> @pop div $total-animals)"/>% of
<cheetah pop="2" /> all <xsl:value-of select="name(..)"/>)
</mammals>
</li>
</zoo>
</xsl:for-each></ul>
</xsl:template>
48
XSLT Variables: variable
Source XML Result HTML
<zoo> <html><head><title>Zoo</title></head><body>
<birds> <ul>
<albatross pop="4" /> <li>albatross (22% of all birds)</li>
<buzzard pop="2" /> <li>buzzard (11% of all birds)</li>
<chickadee pop="12" /> <li>chickadee (67% of all birds)</li>
</birds> </ul>
<mammals> <ul>
<aardvark pop="5" /> <li>aardvark (2% of all mammals)</li>
<bat pop="200" /> <li>bat (97% of all mammals)</li>
<cheetah pop="2" /> <li>cheetah (1% of all mammals)</li>
</mammals> </ul>
</zoo> </body></html>
49
XSLT Extras: sort
XML XSLT fragment
<zoo> <xsl:template match="birds | mammals">
<birds> <ul>
<albatross pop="4" /> <xsl:for-each select="*">
<buzzard pop="2" /> <xsl:sort select="@pop" order="descending"
<chickadee pop="12" /> data-type="number" />
</birds> <li><xsl:value-of select="name(.)" />
<mammals> (<xsl:value-of select="@pop"/>)
<aardvark pop="5" /> </li>
<bat pop="200" /> </xsl:for-each></ul>
<cheetah pop="2" /> </xsl:template>
</mammals>
</zoo>
50
XSLT Extras: sort
Source XML Result HTML
<zoo> <html><head><title>Zoo</title></head><body>
<birds> <ul>
<albatross pop="4" /> <li>chickadee (12)</li>
<buzzard pop="2" /> <li>albatross (4)</li>
<chickadee pop="12" /> <li>buzzard (2)</li>
</birds> </ul>
<mammals> <ul>
<aardvark pop="5" /> <li>bat (200)</li>
<bat pop="200" /> <li>aardvark (5)</li>
<cheetah pop="2" /> <li>cheetah (2)</li>
</mammals> </ul>
</zoo> </body></html>
51
Element Description
Element
apply-imports
apply-templates
Description
Applies a template rule from an imported style sheet
Applies a template rule to the current element or to the current element's child nodes
apply-imports
attribute
attribute-set
Applies a template rule from an imported style sheet
Adds an attribute
choose Used in conjunction with <when> and <otherwise> to express multiple conditional tests
apply-templates
comment
Applies a template rule to the current element or to the current element's
Creates a comment node in the result tree
child nodes
copy Creates a copy of the current node (without child nodes and attributes)
copy-of Creates a copy of the current node (with child nodes and attributes)
attribute-set
fallback Defines a named set of attributes
Specifies an alternate code to run if the processor does not support an XSLT element
include
conditional tests
Includes the contents of one style sheet into another. Note: An included style sheet has the same precedence as
the including style sheet
comment
key
Creates a comment node in the result tree
Declares a named key that can be used in the style sheet with the key() function
namespace-alias Replaces a namespace in the style sheet to a different namespace in the output
copy
number
Creates a copy of the current node (without child nodes and attributes)
Determines the integer position of the current node and formats a number
copy-of Creates a copy of the current node (with child nodes and attributes)
param Declares a local or global parameter
preserve-space Defines the elements for which white space should be preserved
strip-space Defines the elements for which white space should be removed
decimal-format
stylesheet Defines the characters and symbols to be used when converting numbers
Defines the root element of a style sheet
template
text
into strings, with the format-number() function
Rules to apply when a specified node is matched
element
value-of
variable
Creates an element node in the output document
Extracts the value of a selected node
fallback Specifies an alternate code to run if the processor does not support an
52
XSLT element
for-each Loops through each node in a specified node set
include
• .
Includes the contents of one style sheet into another. Note: An included
style sheet has the same precedence as the including style sheet
key Declares a named key that can be used in the style sheet with the key()
function
message Writes a message to the output (used to report errors)
number Determines the integer position of the current node and formats a
number
otherwise Specifies a default action for the <choose> element
strip-space Defines the elements for which white space should be removed
. .
template Rules to apply when a specified node is matched