0% found this document useful (0 votes)
6 views

CH5 WEB Lecture

The document provides an overview of XSL (eXtensible Stylesheet Language), which includes components like XPath, XSLT, and XSLFO for transforming and formatting XML documents. It explains how to apply XSLT stylesheets to XML documents, the structure of XSL templates, and frequently used elements such as <xsl:value-of>, <xsl:for-each>, and <xsl:if> for processing XML data. Additionally, it illustrates examples of XSLT code and how to utilize it for generating HTML output from XML.

Uploaded by

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

CH5 WEB Lecture

The document provides an overview of XSL (eXtensible Stylesheet Language), which includes components like XPath, XSLT, and XSLFO for transforming and formatting XML documents. It explains how to apply XSLT stylesheets to XML documents, the structure of XSL templates, and frequently used elements such as <xsl:value-of>, <xsl:for-each>, and <xsl:if> for processing XML data. Additionally, it illustrates examples of XSLT code and how to utilize it for generating HTML output from XML.

Uploaded by

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

Chapter

Chapter 55

XSL – eXtensible Stylesheet


.Language

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

• XSL is a high-level, functional language


• An XSL style sheet is a valid XML
document
– Valid with respect to the XSL namespace
• Therefore, commands in XSL are XSL
elements

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

The namespace allows the XSL processor to distinguish


between XSL tags and tags of the result document
9
How Does XSLT Work?
• An XSL stylesheet is a collection of
templates that are applied to source nodes
(i.e., nodes of the given XML document)
• Each template has a match attribute that
specifies to which source nodes the
template can be applied
• The current source node is processed by
applying a template that matches this node
• Processing always starts at the root (/)
10
Templates
• A template has the form
<xsl:template match=“/">
...
</xsl:template>
• The content of a template consists of
– XML elements and text that are copied to the
result
– XSL elements that are actually instructions
• / means start of execution.
11
<?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>
<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/<

Applying a browser to catalog.xml


(catalog.xml has a link to catalog.xsl)
13
The Element
<xsl:apply-templates>
• Processing starts by applying a template that
matches the root (/)
– If the given XSL stylesheet does not have a template
that matches the root, then one is inserted by default.
• The XSL stylesheet must specify explicitly
whether templates should be applied to
descendants of the root
• It is done by putting inside a template the
instruction:
<xsl:apply-templates select="xpath"/>
• Without the select attribute, this instruction
processes all the children of the current node
14
<?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/Transform">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<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&lt;10]">
select="catalog/cd[price&lt;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 &gt; 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 &gt;
&gt; 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 &gt; 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 &gt;
&gt; 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&gt;9
test="price&gt;9 and
and price&lt;=10">
price&lt;=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() &lt; 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

• The <xsl:sort> element is used to sort the list of nodes


that are looped over by the <xsl:for-each> element

• Thus, the <xsl:sort> must appear inside the <xsl:for-


each> element

• The looping is done in sorted order

38
Example of <xsl:sort> Element

Sorted by the name of the artist

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

• 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. 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
41
Example of <xsl:if> Element

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

Defines a named set of attributes

call-template Calls a named template

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 Adds an attribute


decimal-format Defines the characters and symbols to be used when converting numbers into strings, with the format-number()
function

element Creates an element node in the output document

attribute-set
fallback Defines a named set of attributes
Specifies an alternate code to run if the processor does not support an XSLT element

call-template Calls a named template


for-each Loops through each node in a specified node set

if Contains a template that will be applied only if a specified condition is true

choose Used in conjunction with <when> and <otherwise> to express multiple


import Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the
importing style sheet

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

message Writes a message to the output (used to report errors)

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

otherwise Specifies a default action for the <choose> element

output Defines the format of the output document

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

processing-instruction Writes a processing instruction to the output

sort Sorts the output

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

Writes literal text to the output

transform Defines the root element of a style sheet

element
value-of

variable
Creates an element node in the output document
Extracts the value of a selected node

Declares a local or global variable

when Specifies an action for the <choose> element

with-param Defines the value of a parameter to be passed into a template

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

if Contains a template that will be applied only if a specified condition is


true
import Imports the contents of one style sheet into another. Note: An imported
style sheet has lower precedence than the importing style sheet

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)

namespace-alias Replaces a namespace in the style sheet to a different namespace in the


output

number Determines the integer position of the current node and formats a
number
otherwise Specifies a default action for the <choose> element

output Defines the format of the output document

param Declares a local or global parameter


preserve-space Defines the elements for which white space should be preserved 53
sort Sorts the output

strip-space Defines the elements for which white space should be removed

stylesheet Defines the root element of a style sheet

. .
template Rules to apply when a specified node is matched

text Writes literal text to the output

transform Defines the root element of a style sheet

value-of Extracts the value of a selected node

variable Declares a local or global variable

when Specifies an action for the <choose> element

with-param Defines the value of a parameter to be passed into a template


54

You might also like