Transcript Slide 1

XSL Transformations
(XSLT)
1
XSLT
• XSLT is a language for transforming XML
documents into XHTML documents or to other
XML documents.
• XSLT uses XPath to navigate in XML
documents.
• An XSLT program is itself an XML document
(called an XSL stylesheet) that describes the
transformation process for input (XML)
documents.
2
XSLT is rule-based
• XSLT is different from conventional
programming languages because XSLT is based
on template rules which specify how XML
documents should be processed.
• The stylesheet declares what output should be
produced when a pattern in the XML document
is matched.
3
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="UK">
<title>Dark Side of the Moon</title>
<artist>Pink Floyd</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Space Oddity</title>
<artist>David Bowie</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Aretha: Lady Soul</title>
<artist>Aretha Franklin</artist>
<price>9.90</price>
</cd>
catalog.xml
</catalog>
4
Style Sheet Declaration
• The root element that declares the document to be an
XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
Note: <xsl:stylesheet> and <xsl:transform> are
completely synonymous and either can be used!
• The correct way to declare an XSL style sheet according
to the W3C XSLT Recommendation is:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
Valid XML!
Includes XHTML
elements
Commands are XML elements
with the namespace xsl
<?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>
<head><title>cd catalog</title></head>
<body><h1>This is a cd catalog!</h1></body>
</html>
</xsl:template>
</xsl:stylesheet>
catalog.xsl
6
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)
• Each template has a match attribute that specifies to
which source nodes the template can be applied
• Each source node has a template that matches it
• The current source node is processed by applying a
template that matches this node
• When processing a node, it is possible (but not
necessary) to recursively process other nodes, e.g., the
children of the processed node
• The XSLT processor processes the document root (/)
7
Templates
• A template has the form
<xsl:template match="pattern">
...
</xsl:template>
• The content of a template consists of
- XML elements (e.g., XHTML) and text that are
copied to the result
- XSL elements (<xsl:…>) that are actually
instructions
• The pattern syntax is a subset of XPath
8
<?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>
<head><title>cd catalog</title></head>
<body><h1>This is a cd catalog!</h1></body>
</html>
</xsl:template>
</xsl:stylesheet>
catalog1.xsl
9
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="catalog1.xsl"?>
<catalog>
<cd country="UK">
<title>Dark Side of the Moon</title>
<artist>Pink Floyd</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Space Oddity</title>
<artist>David Bowie</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Aretha: Lady Soul</title>
<artist>Aretha Franklin</artist>
<price>9.90</price>
</cd>
</catalog>
catalog1.xml
10
11
match
• 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).
• If the path starts with a slash ( / ) it always
represents an absolute path to an element!
12
Examples of Match Attributes
• match="cd",
- All elements with tag name cd
• match="//cd",
match="/catalog/cd/artist"
- All matches of the absolute XPath
• match="cd/artist"
- All artist nodes that have a cd parent
• match="catalog//artist"
- All artist nodes that have a catalog ancestor
• match="cd[@country='UK']/artist"
13
<?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>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th> <th>Artist</th> </tr>
<tr> <td>.</td> <td>.</td> </tr>
</table>
</body> </html>
</xsl:template>
</xsl:stylesheet>
14
Frequently Used Elements of XSL
• <xsl:value-of select="xpath"/>
- This element extracts the value of a node from the node
list located by xpath
• <xsl:for-each select="xpath"/>
- This element loops over all the nodes in the node list
located by xpath
• 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
15
<?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>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<tr> <td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td> </tr>
</table> </body> </html>
</xsl:template>
</xsl:stylesheet>
16
<?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>
<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="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td> </tr>
</xsl:for-each>
</table> </body> </html>
</xsl:template>
</xsl:stylesheet>
17
<?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>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
<tr> <td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td> </tr>
</xsl:for-each>
</table> </body> </html>
</xsl:template>
</xsl:stylesheet>
18
<?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>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr> <td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</xsl:for-each>
</table> </body> </html>
</xsl:template>
</xsl:stylesheet>
19
Frequently Used Elements of XSL (cont)
• <xsl:if test="cond"/>,
<xsl:if test="xpath"/>, etc.
- This element is for conditional processing
• The <xsl:choose> element is used in
conjunction with <xsl:when> and
<xsl:otherwise> to express multiple
conditional tests.
20
<?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>
<table border="1">
<tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<tr> <td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td> </tr>
</xsl:if>
</xsl:for-each>
</table> </body> </html>
</xsl:template>
</xsl:stylesheet>
21
<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>
22
<xsl:apply-templates>
• Tells the processor to continue
processing other nodes in the input
document and instantiate their
matching templates.
• Processing starts by applying a
template to the root.
- If no specified template matches the root,
then one is inserted by default (see slide 25)
23
<xsl:apply-templates>
(cont)
• The XSL stylesheet must specify explicitly whether
templates should be applied to descendants of a
node.
This is done by putting inside a template the
instruction:
<xsl:apply-templates select="xpath"/>
In xpath, the current processed node is used as the context node
• Without the select attribute, this instruction
processes all the children of the current node
(including text nodes).
24
Default Templates
• XSL provides implicit built-in templates that match
every element and text nodes.
selects from the root node and
matches any element node
<xsl:template match="/|*">
<xsl:apply-templates/>
</xsl:template>
matches text &
attributes nodes
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
• Templates we write always override these built-in
templates (when they match).
25
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
Dark Side of the Moon
Pink Floyd
10.90
Space Oddity
No template!
default template
is used
David Bowie
9.90
…
26
<?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="cd">
<h2>A cd!</h2>
</xsl:template>
</xsl:stylesheet>
<h2>A cd!</h2>
<h2>A cd!</h2>
The template
applies to all of
the CDs.
<h2>A cd!</h2>
27
<?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="cd[@country='UK']">
<h2>A cd!</h2></xsl:template>
</xsl:stylesheet>
<h2>A cd!</h2>
<h2>A cd!</h2>
Aretha: Lady Soul
Aretha Franklin
9.90
The template
doesn’t apply to
Aretha Franklin’s
CD, default
template used here.
28
<xsl:stylesheet version="1.0" Matches the root –
default template
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
will not be applied
<xsl:template match="/">
<xsl:apply-templates
select="catalog/cd[@country='UK']/artist"/>
</xsl:template>
<xsl:template match="artist">
<h2>An artist!</h2>
</xsl:template>
</xsl:stylesheet>
<h2>An artist!</h2>
<h2>An artist!</h2>
instead of outputting a
fixed response, it
instantiates the
template for each
matching artist element
29
<xsl:stylesheet version="1.0" Matches the root –
default template
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
will not be applied
<xsl:template match="/">
<xsl:apply-templates
select="cd[@country='UK']/artist"/>
</xsl:template>
<xsl:template match="artist">
<h2>An artist!</h2>
</xsl:template>
</xsl:stylesheet>
No match!
“catalog” is
missing
30
Setting Values in Attributes
• We can insert expressions into attribute values,
by putting the expression inside curly braces
({}).
• Alternatively, we can use <xsl:element> in
order to construct XML elements.
31
An Example
• In the following example, we add to each CD entitled
”title” a link to the URL /showcd.html?title= ”title”
<xsl:template match="cd">
<b><xsl:value-of select="artist"/></b>:
<a href="/showcd.html?title={./title}">
<xsl:value-of select="title"/>
</a>
</xsl:template>
32
33
Using <xsl:element>
<xsl:template match="cd">
<b><xsl:value-of select="artist"/></b>:
<xsl:element name="a">
<xsl:attribute name="href">
showcd/?title=<xsl:value-of select="title"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</xsl:element>
</xsl:template>
34
On XSL Code
• Typically, an XSLT program can be written in
several, very different ways
- Templates can sometime replace loops and vice versa
- Conditions can sometimes be replaced with XPath
predicates (e.g., in the select attribute) and vice versa
• A matter of convenience and elegancy
35
Identity Transformation
Stylesheet
• Suppose that we want to write an XSL stylesheet
that generates a copy of the source document
- It is rather easy to do it when the structure of the
source XML document is known
• Can we write an XSL stylesheet that does it for
every possible XML document?
- Yes! (see next slide)
36
<?xml version="1.0"?>
defines the format of
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
the output document
version="1.0">
<xsl:output method="xml"/>
any element node
<xsl:template match="*">
any attribute node
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
Identity Transformation
</xsl:template>
</xsl:stylesheet>
Stylesheet
37
Generating Valid XHTML
• By default, the documents that XSL stylesheets
generate are not valid XHTML
• Next, we will show how XSL stylesheet can be
changed in order to generate valid XHTML
38
The Original XSL Example
<?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>
<head><title>cd catalog</title></head>
<body><h1>This is a cd catalog!</h1></body>
</html>
</xsl:template>
</xsl:stylesheet>
39
Modifying the XSL Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output
method="html"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system=
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<xsl:template match="/"> …
40
The Transformation Result
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>cd catalog</title>
</head>
<body>
<h1>This is a cd catalog!</h1>
</body>
</html>
41
Some Other XSL Elements
• The <xsl:text> element inserts free text
in the output
• The <xsl:copy-of select="xpath">
creates a copy of the specified nodes, to insert
a result tree fragment into the result tree
• The <xsl:comment> element creates a
comment node in the result tree
• The <xsl:variable> element defines a
variable (local or global) that can be used
within the program
42
The End!
These slides are based on:
•
Slides developed for the course:
http://www.cs.huji.ac.il/~dbi
•
Tutorial:
http://www.w3schools.com/xsl/
43