XSLT
XSLT
Transformation
Ahmed ZELLOU
[email protected]
Sup-MTI, 2024-2025.
XSLT
XSL
2017.
3
@ A.ZELLOU, XML Technology
XSLT
Introduction
4
@ A.ZELLOU, XML Technology
XSLT
XSLT Transformation
n Namespace
n xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
7
@ A.ZELLOU, XML Technology
XSLT
XSLT Transformation
9
@ A.ZELLOU, XML Technology
XSLT
The <xsl:template> element
n Input
<note>He teaches<mat>XML</mat></note>
n Script
<xsl:template match="note">
<xsl:value-of select="."/>
</xsl:template>
n Output
He teaches XML
11
@ A.ZELLOU, XML Technology
XSLT
The text() element
n Input
<note>He teaches<mat>XML</mat></note>
n Script
<xsl:template match="note">
<xsl:value-of select="text()"/>
</xsl:template>
n Output
He teaches
14
@ A.ZELLOU, XML Technology
XSLT
The <xsl:sort> element
16
@ A.ZELLOU, XML Technology
XSLT
Tests
n Example :
n <xsl:for-each select="Library/Book[author='Choukri']">
n ! = (Different)
17
@ A.ZELLOU, XML Technology
XSLT
Tests
n Syntax
<xsl:if test="expression">
... result if the test is true ...
</xsl:if>
19
@ A.ZELLOU, XML Technology
XSLT
The <xsl:if> element
Example
<?xmlversion="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheetversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th> <th>Author</th> This example adds a purple background to
</tr> the "Author" column when the book price is
<xsl:for-each select="Library/Book"> greater than 10.
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:if test="price > 10">
<td bgcolor="#ff00ff"><xsl:value-of select="author"/></td>
</xsl:if>
<xsl:if test="price ≤ 10">
<td bgcolor="#00ff00"><xsl:value-of select="author"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table> </body> </html> 21
</xsl:template> </xsl:stylesheet> @ A.ZELLOU, XML Technology
XSLT
The <xsl:choose> element
n Syntax
<xsl:choose>
<xsl:when test="expression">
... Output elements ...
</xsl:when>
<xsl:otherwise>
... Output elements ....
</xsl:otherwise>
</xsl:choose>
22
@ A.ZELLOU, XML Technology
XSLT
The <xsl:choose> element
24
@ A.ZELLOU, XML Technology
XSLT
Output Types
n Three types of output document:
n xml: default output
<xsl:output method="xml">
n html :
<xsl:output method="html">
n By default, xml, unless the first tag in the result tree is <html>.
25
@ A.ZELLOU, XML Technology
XSLT
The <xsl:element> element
<xsl:template match="path">
<xsl:element name="name">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:template>
26
@ A.ZELLOU, XML Technology
XSLT
The <xsl:attribute> element
n Allows to create an attribute
<xsl:output method="xml" indent="yes"/>
<xsl:template match="path">
<xsl:element name="name">
<xsl:value-of select="value"/>
<xsl:attribute name="name">
<xsl:value-of select="value"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
27
@ A.ZELLOU, XML Technology
XSLT
The element { }
n Input
<a href="file.txt"/>
n Script
<xsl:template match="a">
<b id="{@href}"/>
</xsl:template>
n Output
<b id="file.txt"/>
n Other script
<xsl:template match="a">
<b><xsl:attribut name="id">
<xsl:value-of select="@href"/>
</xsl:attribut</b>
28
</xsl:template> @ A.ZELLOU, XML Technology
XSLT
The name() element
n Input
<note>He teaches
<mat>XML</mat>
</note>
n Script
<xsl:template match=".">
<xsl:value-of select="name()"/>
</xsl:template>
n Output Shaft
note
29
@ A.ZELLOU, XML Technology
XSLT
The <xsl:variable> element
n Reference to a variable
<BODY BGCOLOR='{$backcolor}'>
n NB
n A variable cannot be reassigned (constants)
30
@ A.ZELLOU, XML Technology
XSLT
Passing parameters
31
@ A.ZELLOU, XML Technology
XSLT
Passing parameters
<xsl:stylesheet>
<xsl:param name="dir" select="'myDir'"/>
...
</xsl:stylesheet>
n Equivalent to
dir='myDir'
32
@ A.ZELLOU, XML Technology
XSLT
The <xsl:number> element
33
@ A.ZELLOU, XML Technology
XSLT
The <xsl:message> element
n Input
<xsl:message>
code= <xsl:value-of select="$code"/>
</xsl:message>
n Output
code = 25
34
@ A.ZELLOU, XML Technology
XSLT
The <xsl:copy-of> element
35
@ A.ZELLOU, XML Technology
XSLT
<xsl:comment> & <xsl:import>
n To get the comments out
<xsl:template match="comment()">
<xsl:comment>
<xsl:value-of select="."/>
</xsl:comment>
</xsl:template>
n Manage spaces
n <xsl:strip-space>
n does not preserve spaces in elements
n <xsl:preserve-space>
n preserves spaces in elements
37
@ A.ZELLOU, XML Technology
XSLT
Others
n <xsl:key>
n Associate an identifier to a node
n <xsl:decimal-format>
n Define a model for representing decimal numbers
(useful if internationalization)
n <xsl:attribute-set>
n Declares a set of reusable attributes
38
@ A.ZELLOU, XML Technology
XSLT
The Templates
n Recursive
<xsl:apply-templates>
n Statement
<xsl:template name="...">
...
</xsl:template>
n Call
<xsl:call-template> or <xsl:apply-templates>
39
@ A.ZELLOU, XML Technology
XSLT
<xsl:apply-templates> element
n The element <xsl:apply-templates> applies a template.
40
A.ZELLOU
XSLT
The Templates
n Example
<xsl:template name="navbar">
<a href="index.htm">Home</a>|
<a href="help.htm">Help</a> |
<a href="toc.htm">Contents</a>
</xsl:template>
<xsl:template match="mainblock">
<body>
<xsl:call-template name="navbar"/>
<xsl:apply-templates/>
<xsl:call-template name="navbar"/>
</body>
</xsl:template>
41
@ A.ZELLOU, XML Technology
XSLT
<xsl:apply-templates> element
n Example:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" Which gives as a result
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Books Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Biblio/Book">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="author"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="author">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br /> 42
</xsl:template></xsl:stylesheet>
A.ZELLOU
XSLT
XSLT - reference
Element Description
apply-imports Applies a template rule from an imported style sheet
apply-templates Applies a template rule to the current element or to the child node of the
current element
attribute Add an attribute
attribute-set Defines a named set of attributes
call template Calls a named model
choose Used with <when> and <otherwise> to express multiple conditional tests
comment Creates a comment node in the result tree
copy Creates a copy of the current node (without child nodes and without attributes)
copy-of Creates a copy of the current node (with child nodes and attributes)
decimal-format Defines the characters and symbols to use when converting numbers to strings
with the format-number() function.
element Creates an element node in the resulting document
fallback Specifies an alternative code to execute if the processor
43
does not support an
XSLT element.
A.ZELLOU
XSLT
XSLT - reference
Element Description
for-each Loop through each node in a specified node set
if Contains a template that will be applied if a specified condition is true
import Imports the contents of one style sheet into another. Note: An
imported style sheet has lower priority than the main style sheet.
include Include the contents of one style sheet within another. Note: An
included style sheet has the same priority as the main style sheet.
key Declares a named key that can be used in the stylesheet with the key()
function
message Writes a message on exit (used to report errors)
namespace-alias Replaces a namespace in the stylesheet with another namespace in the
result
number Determines the position of the current node (returns a number)
otherwise Specifies a default action for the <choose> element
output Sets the format of the result document
44
A.ZELLOU
XSLT
XSLT - reference
Element Description
param Declares a parameter
preserve-space Defines the elements for which spaces should be preserved
processing Write a processing instruction
instruction
sort Sort
strip space Defines the elements for which white spaces should be removed
stylesheet Defines the root element of the style sheet
template Rules to apply when a specified node is found
text Write a literal text
transform Sets the root element of the style sheet
value-of Extracts the value of a selected node
variable Declare a variable
when Specifies an action for the <choose> element
with-param Sets the value of a parameter to pass to a template 45
A.ZELLOU
XSLT
Name Description
current() Returns the current node
document() Used to access nodes in an external XML document
element- Tests if an element is supported by the XSLT processor
available()
format-number() Converts a number to a string
function- Tests if a function is supported by the XSLT processor
available()
generate-id() Returns a string that uniquely identifies a specified node
key() Returns a set of nodes using the index specified by an <xsl:key> element
system-property() Returns system properties
unparsed-entity- Returns the URI of an unparsed entity
uri()
46
A.ZELLOU
XSLT
Conclusion
n XSLT is a language for transforming XML documents
into XHTML or other XML documents.
n The most important part of XSL.
n Uses XPath to navigate XML documents
n Allows to show, hide, reorganize, sort, perform tests
on elements and attributes of the resulting file.
n XSLT is a real programming language
n XSLT has no equivalent language for tree
transformation
n But, developing XSLT programs can be "hard"
47
A.ZELLOU
XSLT
Workshop
1. Create an XML file "books.xml" containing a collection of
books.
a. A Library contains multiple books. A book contain a title,
an author, a year, a price and an ISBN attribute.
2. Write an XSLT stylesheet to transform the books.xml file into
an HTML document. The output should contain a table where:
a. Each book is a row.
b. The columns should include Title, ISBN, Author and Price.
c. Sort by Year.
d. Add an additional column for a formatted Discount Price
(e.g., 90% of the price).
48
A.ZELLOU
End