You are on page 1of 32

XSLT 2

Lecture 5

XSLT video: https://www.youtube.com/watch?


Template
 The basics of XSLT is to create templates that match the nodes
in the structure.
 A node -> elements, attributes, text, namespaces, processing-
instructions, and comments.
 Two type of templates:
 Match (Unnamed) Templates
 Named Templates
 When XSLT processes a document, it recursively traverses the
entire document hierarchy, inspecting each node, looking for a
template that matches the current node.
 When a matching template is found, the contents of that
template are evaluated.
I. Match Template
 Match templates, also known as unnamed templates.
 <xsl:template> is used to build a template
<xsl:template match="XPath expression">
 The match attribute specifies which node the template should be invoked.
 XPath expression identifies the source node in the XML document.

 <xsl:apply-templates> applies a template rule to the current


element or to the current element's child nodes.
<xsl:apply-template select="XPath expression">
 The select attribute traverses the node that matches XPath expression.
 If select attribute not provided, it traverses all children of the context node.
 If the select attribute is included, but does not match any nodes, nothing is
traversed and nothing happens.
Example of Match Template
It match the root node (entire document)
XSL
<xsl:template match="/">
...
XML
</xsl:template>
<?xml version="1.0"?>
It match all <para> element nodes <title>My Title</title>
<para>A source paragraph.</para>
XSL
<note>This is a note.</note>
<xsl:template match="title/para"> <para>Another source paragraph.</para>
... </title>
</xsl:template>
Example of Match Template 1
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" indent="yes"/>

<xsl:template match="title"> XML


<record><xsl:apply-templates/></record> <?xml version="1.0"?>
</xsl:template> <title>
<para>A source paragraph.</para>
<xsl:template match="note"> <note>This is a note.</note>
<p><xsl:apply-templates/></p> <para>Another source paragraph.</para>
</xsl:template> </title>

</xsl:stylesheet>
Output (XML)
• xsl:template match="title"  defined the root <?xml version="1.0"?>
<record>
element as title element. A source paragraph.
• xsl:apply-templates  check for template <p>This is a note.</p>
matches on all the children of title, which is para Another source paragraph.
and note. </record>
Example of Match Template 2
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<xsl:element name="chapter">
XML
<xsl:apply-templates select="title/para"/>
</xsl:element> <?xml version="1.0"?>
</xsl:template> Appl <title>
y <para>A source paragraph.</para>
<xsl:template match="para"> <note>This is a note.</note>
<xsl:element name="new"> <para>Another source paragraph.</para>
<xsl:value-of select="."/> </title>
</xsl:element>
</xsl:template> Output (XML)
</xsl:stylesheet> <?xml version="1.0" encoding="UTF-8"?>
<chapter>
<new>A source paragraph.</new>
<new>Another source paragraph.</new>
</chapter>
<xsl:element> and <xsl:attribute>
 <xsl:element> create an element node in the output
document.
<xsl:element name="new element name">
 The name attribute specifies name of the element to be created.

 <xsl:attribute> create an attribute node in the output


document.
<xsl:attribute name="new attribute name">
 The name attribute specifies name of the attribute to be created.
<xsl:variable>
 <xsl:variable> element declare a local or global variable.
 The variable is global if it is declared as a top-level
element, and local if it is declared within a template.
Literal values is defined as a
string values enclosed in quotes
<xsl:variable name="color" select="'red'" />

<xsl:variable name="price" /> the value of the variable is an empty string

<xsl:variable name='discount' select='2+3'/>

<xsl:template match='para'>
<xsl:value-of select='$discount'/>
</xsl:template>
<xsl:value-of>
 <xsl:value-of> extracts the value of a selected node
through the XPath expression
<xsl:value-of select="XPath expression"/>
 Example:
 The <xsl:value-of select= "." /> element extracts value from current node
in source document
 The <xsl:value-of select= ".." /> element extracts value from the parent
of the current node in source document
 The <xsl:value-of select= "title" /> element extracts value from <title>
element in source document
 The <xsl:value-of select= "@id" /> element extracts value from id
attribute in source document

Source picture: http://www.xmlmaster.org/en/article/d01/c07/


Example of Match Template 3
<?xml version="1.0"?> XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XML
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="/"> <?xml version="1.0"?>
<xsl:element name="record"> <root>
<xsl:apply-templates select="//item"/> <stock>
</xsl:element> <item>Pencil</item>
</xsl:template> Apply <price>RM3.20</price>
</stock>
<xsl:template match="item"> <stock>
<xsl:element name="product"> <item>A4 paper</item>
<xsl:attribute name="cost"> <price>RM11.50</price>
<xsl:value-of select="../price"/> </stock>
</xsl:attribute> </root>
<xsl:attribute name="category">stationary</xsl:attribute>
<xsl:value-of select="."/> Output (XML)
</xsl:element>
</xsl:template> <?xml version="1.0"?>
<record>
</xsl:stylesheet> <product cost="RM3.20" category="stationary">Pencil</product>
<product cost="RM11.50" category="stationary">A4
paper</product>
</record>
II. Named Template
 Named templates operate like functions in traditional
programming languages.
 It accept arguments and run only when explicitly called.
<xsl:template name="template name">
 The name attribute similar to a function name in traditional programming
languages.
 <xsl:param> is used to define a parameter in a template
<xsl:param name="parameter" select="default value">
 name attribute defines parameters for the named template,
 select attribute declares default values for each parameter (it may contain
XPath expressions)
 If the select attribute is not defined, the parameter defaults to an empty
string.
Example of Named Template
<xsl:template name="my-template">
<xsl:param name="a" select="'false'"/>
<xsl:param name="b" select="book"/>
<xsl:param name="c"/>
<!-- ... body of the template ... -->
</xsl:template>

• The name of the template: my-template


• It defines three parameters:
• parameter a has default content of string false
• parameter b has defaults contents of the element node named book that
is a child of the current node.
• parameter c does not have default content
• If the script calls the template and does not pass in a parameter, the default
value is used.
II. Named Template cont..
 <xsl:call-template> invokes a named template in a script.
<xsl:call-template name="template name">

 The name attribute is required and it defines the name of the template
being called.

 <xsl:with-param> pass arguments into the template.


<xsl:with-param name="parameter name" select="value">

 The value of the name attribute must match the parameter defined in
the actual template; otherwise the parameter is ignored.
Example of Named Template 1
<?xml version="1.0"?> XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
XML
<xsl:output method="xml" indent="yes"/> <?xml version="1.0"?>
<xsl:template match="/"> <area>
<xsl:element name="record"> <shop>
<xsl:for-each select="area/shop"> <name>ABC</name>
<xsl:call-template name="test" /> <location>Bukit Baru</location>
</xsl:for-each> <state>Melaka</state>
</xsl:element> </shop>
</xsl:template> <shop>
<name>XYZ</name>
<xsl:template name="test"> <location>Tanjung Luar</location>
<xsl:element name="target"> <state>Selangor</state>
<xsl:apply-templates select="name"/> </shop>
</xsl:element> </area>
<xsl:element name="place"> Output (XML)
<xsl:text>The location is at </xsl:text> <?xml version="1.0"?>
<xsl:apply-templates select="state"/> <record>
</xsl:element> <target>ABC</target>
</xsl:template> <place>The location is at Melaka</place>
<target>XYZ</target>
</xsl:stylesheet> <place>The location is at Selangor</place>
</record>
Example of Named Template 2
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
XSL
<xsl:output method= "text" /> XM
<xsl:template name="test">
<xsl:param name='value'/> <?xml version="1.0"?> L
<xsl:param name='charges'/>
<xsl:text>&#xa;The total is RM</xsl:text> <fruit name="Apple">
<xsl:value-of select='$value + $charges '/> <shop>fruit shop</shop>
</xsl:template> <location>Bukit
Baru</location>
<xsl:template match='/'> <state>Melaka</state>
<xsl:text>Fruit: </xsl:text> <price>8.50</price>
<xsl:value-of select="fruit/@name" /> <delivery>1.50</delivery>
<xsl:text>&#xa;Shop location: </xsl:text> </fruit>
<xsl:value-of select="fruit/location"/> Output (text)
<xsl:text>, </xsl:text>
<xsl:value-of select="//state"/> Fruit: Apple
<xsl:text>&#xa;</xsl:text> Shop location: Bukit Baru, Melaka
<xsl:call-template name='test'>
<xsl:with-param name='value' select='fruit/price'/> The total is RM10
<xsl:with-param name='charges' select='//delivery'/>
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
Example of
<xsl:output method="html"/> Named
<xsl:template match="/">
<html>
<body>
Template 3
<h3>Team Members</h3> XM
<ul> <?xml version="1.0"?> L
<xsl:for-each select="team/leader|team/member"> <team>
<xsl:sort select="name"/> <leader ic="112301222">
<li> It generate a text node to the result
<name>Muthu</name>
<xsl:value-of select="name"/> document, the value inside the <xsl:text>
</leader>
<xsl:text>, ic = </xsl:text> printed as a string to output.
<member ic="106230133">
<xsl:call-template name="formatIC"> <name>Anna</name>
<xsl:with-param name="ic" select="@ic"/> </member>
</xsl:call-template> <member ic="128230111">
</li> <name>Aminah</name>
</xsl:for-each> </member>
</ul> call “formatIC” template and pass </team>
</body> the ic parameter to the template
</html>
</xsl:template> Output (HTML code)
<xsl:template name="formatIC"> <html><body>
<xsl:param name="ic"/> <h3>Team Members</h3>
<xsl:value-of select="substring($ic, 1, 3)"/> <ul>
<xsl:text>-</xsl:text> Output (HTML) <li>Aminah, ic = 128-23-0111</li>
<xsl:value-of select="substring($ic, 4, 2)"/> <li>Anna, ic = 106-23-0133</li>
<xsl:text>-</xsl:text> <li>Muthu, ic = 112-30-1222</li>
<xsl:value-of select="substring($ic, 6)"/> </ul>
</xsl:template> </body></html>

</xsl:stylesheet>
<xsl:for-each>
 <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
<xsl:for-each select="XPath expression"/>
<xsl:template match='/'>
<html>
<body>
<ol>
<xsl:for-each select='team/leader|team/member'>
<li><xsl:value-of select='@ic'/></li>
</xsl:for-each>
</ol>
</body>
</html>
</xsl:template>
<xsl:sort>
 <xsl:sort> element sort elements in a variety of ways.
 The default for the sort element is to sort alphabetically
 Positioning the sort element: place the sort element immediately
after an element whose elements that want sorted
<xsl:for-each select="team/leader|team/member">
<xsl:sort select="name"/>
 Sorting in reverse alphabetical order: use the order attribute:
<xsl:sort select="name" order="descending" />
 Sorting numerically: use the data-type attribute
<xsl:sort select= "salary" data-type="number" />
 Sorting using a primary key and a secondary key: cascade the
sort operators. <xsl:sort select="salary" data-type="number" />
<xsl:sort select="name" />
<?xml version="1.0"?>
XSL Example <team>
<leader ic="112301222">
<?xml version="1.0" encoding="UTF-8"?> <name>Muthu</name>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <salary>6000</salary>
</leader>
<xsl:output method="html"/> XM <member ic="106230133">
<xsl:template match="/"> L <name>Anna</name>
<html> <salary>3000</salary>
<body> </member>
<h4>Team members (descending order)</h4> <member ic="128230111">
<ul> <name>Aminah</name>
<xsl:for-each select="team/leader|team/member"> <salary>4500</salary>
<xsl:sort select="name" order="descending"/> </member>
<li> <member ic="114789331">
<xsl:value-of select="name"/> <name>Steven</name>
</li> <salary>4500</salary>
</xsl:for-each> </member>
</ul> </team>
<hr/>
<h4>Team members based on salary</h4>
<ul>
<xsl:for-each select="team/leader|team/member">
<xsl:sort select="salary" data-type="number"/>
<xsl:sort select="name" />
<li>
<xsl:value-of select="name"/> - RM<xsl:value-of select="salary"/>
</li>
</xsl:for-each>
</ul>

</body>
</html>
</xsl:template>
Output (HTML)
</xsl:stylesheet>
<xsl:if>
 The <xsl:if> element is used for conditional processing.
 The condition appears as the value of the test attribute.
<xsl:template match="shop"> <xsl:template match="shop">
<xsl:if test="state = 'Melaka'"> <xsl:if test="price &gt; 10">
... ...
</xsl:if> </xsl:if>
</xsl:template> </xsl:template>

 The instruction inside the <xsl:if> element are processed if


the condition is true.
<xsl:if>
Conditional Expressions

Source: http://faculty.madisoncollege.edu/schmidt/xml/xmlcond.html
Example (if-statement) 1
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html"/> XM
<xsl:template match='/'>
<html> <?xml version="1.0"?> L
<body> <record>
<xsl:for-each select='record/member'> <member name="Philip" year="2010">
<country>Taiwan</country>
<xsl:if test="@year &gt; '2010'"> </member>
<p>Country is <xsl:value-of select='country'/></p> <member name="Mary" year="2012">
</xsl:if> <country>Malaysia</country>
</member>
</xsl:for-each> <member name="Jimmy" year="2015">
</body> <country>Singapore</country>
</html> </member>
</xsl:template> </record>

</xsl:stylesheet>
Output (HTML code)
Output (HTML) <html>
<body>
Country is Malaysia <p>Country is Malaysia</p>
Country is Singapore <p>Country is Singapore</p>
</body>
</html>
Example (if-statement) 2
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
XM
<xsl:output method="html"/> <?xml version="1.0"?> L
<xsl:template match='/'> <record>
<html> <member name="Philip" year="2010">
<body> <country>Taiwan</country>
<xsl:for-each select='record/member'> </member>
<member name="Mary" year="2012">
<xsl:if test="(@name='Philip') or (@year &gt; '2011')"> <country>Malaysia</country>
<p>Country is <xsl:value-of select='country'/></p> </member>
</xsl:if> <member name="Jimmy" year="2015">
<country>Singapore</country>
</xsl:for-each> </member>
</body> </record>
</html>
</xsl:template>
Output (HTML code)
</xsl:stylesheet> <html>
Output (HTML) <body>
<p>Country is Taiwan</p>
Country is Taiwan <p>Country is Malaysia</p>
<p>Country is Singapore</p>
Country is Malaysia </body>
Country is Singapore </html>
<xsl:choose>
 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
<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 the XSL document
like this:
<xsl:choose>
<xsl:when test="price &gt; 10">
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ....
</xsl:otherwise>
</xsl:choose>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Example
<xsl:output method="text"/> <?xml version="1.0"?> XM
<products>
<xsl:template match="/"> <make area="Europe">study desk</make> L
<xsl:for-each select="products/make"> <make area="North America">3 seater sofa</make>
<xsl:text>&#xA; Product: </xsl:text> <make area="North America">L shaped sofa</make>
<xsl:value-of select="."/> <make area="Asia">kitchen cabinet</make>
<make area="Asia">storage rack</make>
<xsl:choose> <make area="?">TV cabinet</make>
<xsl:when test="@area = 'North America'"> </products>
<xsl:text>&#xA; - Sofa seat&#xA;</xsl:text>
</xsl:when>
<xsl:when test="@area = 'Europe'"> Output (text)
<xsl:text>&#xA; - Import from Europe&#xA;</xsl:text> Product: study desk
</xsl:when> - Import from Europe
<xsl:when test="@area = 'Asia'">
<xsl:text>&#xA; - It's from Asia&#xA;</xsl:text> Product: 3 seater sofa
</xsl:when> - Sofa seat
<xsl:otherwise>
<xsl:text>&#xA; - We don't know&#xA;</xsl:text> Product: L shaped sofa
</xsl:otherwise> - Sofa seat
</xsl:choose>
Product: kitchen cabinet
</xsl:for-each> - It's from Asia
</xsl:template>
Product: storage rack
</xsl:stylesheet> - It's from Asia

Product: TV cabinet
- We don't know
Count Function
 count() is the predefined function in XSLT.
 It returns the total number of input nodes in a sequence.
 It can be used to count elements with specific attribute
names defined in the XML document.
<xsl:value-of select="count(products/make)"/>

<xsl:value-of select="count(products/make[@area='Asia'])"/>

<xsl:value-of select="count(products/make[@area='Europe'])"/>

<?xml version="1.0"?> XM
<products>
<make area="Europe">study desk</make> L
<make area="North America">3 seater sofa</make>
<make area="North America">L shaped sofa</make>
<make area="Asia">kitchen cabinet</make>
<make area="Asia">storage rack</make>
<make area="?">TV cabinet</make>
</products>
Example (Count Function)
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:text>&#xA; Total product: </xsl:text>
<xsl:value-of select="count(products/make)"/>

<xsl:text>&#xA; Product from Asia: </xsl:text>


<xsl:value-of select="count(products/make[@area='Asia'])"/>

</xsl:template>

</xsl:stylesheet>

<?xml version="1.0"?> XM Output (text)


<products>
<make area="Europe">study desk</make> L Total product: 6
<make area="North America">3 seater sofa</make> Product from Asia: 2
<make area="North America">L shaped sofa</make>
<make area="Asia">kitchen cabinet</make>
<make area="Asia">storage rack</make>
<make area="?">TV cabinet</make>
</products>
XSLT with CSS
 CSS (Cascading Style Sheets) can be used to add style
and display information to an XML document.
 It can format the whole XML document.
 There are 3 types:
 Inline style
 Embedded style
 External style
CSS Inline Style Example

member.XSL
Member.XML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <?xml version="1.0"?>
<?xml-stylesheet
<xsl:output method="html"/> href="member.xsl"
<xsl:template match="/"> type="text/xsl"?>
<html> <team>
<head><title>Member Info</title> <leader ic="112301222">
</head> <name>Muthu</name>
<salary>6000</salary>
<body>
</leader>
<h4 style="font-size:20pt; text-decoration:underline;">Team members based on salary</h4>
<member ic="106230133">
<ul style="color:blue; font-style: italic;"> <name>Anna</name>
<salary>3000</salary>
<xsl:for-each select="team/leader|team/member"> </member>
<xsl:sort select="salary" data-type="number"/> <member ic="128230111">
<xsl:sort select="name" /> <name>Aminah</name>
<li><xsl:value-of select="name"/> - RM<xsl:value-of select="salary"/></li> <salary>4500</salary>
</xsl:for-each> </member>
</ul> <member ic="114789331">
</body> <name>Steven</name>
</html>
Output (HTML) <salary>4500</salary>
</xsl:template>
</xsl:stylesheet> </member>
</team>
member1.XSL CSS Embedded Style Example
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <?xml-stylesheet
href="member1.xsl"
<xsl:output method="html"/> type="text/xsl"?>
<xsl:template match="/"> <team>
<html> <leader ic="112301222">
<head><title>Member Info</title> Member1.XML <name>Muthu</name>
<style> <salary>6000</salary>
h4{ </leader>
font-size: 20pt; <member ic="106230133">
text-decoration: underline; <name>Anna</name>
} <salary>3000</salary>
ul{ </member>
color: blue; <member ic="128230111">
font-style: italic; <name>Aminah</name>
} <salary>4500</salary>
</style> </member>
</head> <member ic="114789331">
<name>Steven</name>
<body> <salary>4500</salary>
<h4>Team members based on salary</h4> </member>
<ul> </team>
<xsl:for-each select="team/leader|team/member">
<xsl:sort select="salary" data-type="number"/>
<xsl:sort select="name" /> Output (HTML)
<li><xsl:value-of select="name"/> - RM<xsl:value-of select="salary"/></li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
CSS External Style Example
member2.XSL
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <?xml-stylesheet
href="member2.xsl"
<xsl:output method="html"/> type="text/xsl"?>
<xsl:template match="/"> <team>
<html> <leader ic="112301222">
<head> Member2.XML <name>Muthu</name>
<title>Member Info</title> <salary>6000</salary>
<link rel="stylesheet" type="text/css" href="member2.css" /> </leader>
</head> <member ic="106230133">
<name>Anna</name>
<body> <salary>3000</salary>
<h4>Team members based on salary</h4> </member>
<ul> <member ic="128230111">
<xsl:for-each select="team/leader|team/member"> <name>Aminah</name>
<xsl:sort select="salary" data-type="number"/> <salary>4500</salary>
<xsl:sort select="name" /> </member>
<li><xsl:value-of select="name"/> - RM<xsl:value-of select="salary"/></li> <member ic="114789331">
</xsl:for-each> <name>Steven</name>
</ul> <salary>4500</salary>
</body> </member>
</html> </team>
</xsl:template> member2.CSS
</xsl:stylesheet>
h4{ Output (HTML)
font-size: 20pt;
text-decoration: underline;
}
ul{
color: blue;
font-style: italic;
}

You might also like