Professional Documents
Culture Documents
Lecture 5
</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: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
The name attribute is required and it defines the name of the template
being called.
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>
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>
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>
</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 > 10">
... ...
</xsl:if> </xsl:if>
</xsl:template> </xsl:template>
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 > '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 > '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 > 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>
 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>
 - Sofa seat
</xsl:text>
</xsl:when>
<xsl:when test="@area = 'Europe'"> Output (text)
<xsl:text>
 - Import from Europe
</xsl:text> Product: study desk
</xsl:when> - Import from Europe
<xsl:when test="@area = 'Asia'">
<xsl:text>
 - It's from Asia
</xsl:text> Product: 3 seater sofa
</xsl:when> - Sofa seat
<xsl:otherwise>
<xsl:text>
 - We don't know
</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>
 Total product: </xsl:text>
<xsl:value-of select="count(products/make)"/>
</xsl:template>
</xsl:stylesheet>
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;
}