Lecture 5
Lecture 5
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> Apply <title>
<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 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" /> XML
<xsl:template name="test">
<xsl:param name='value'/> <?xml version="1.0"?>
<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>
<state>Melaka</state>
<xsl:template match='/'> <price>8.50</price>
<xsl:text>Fruit: </xsl:text> <delivery>1.50</delivery>
<xsl:value-of select="fruit/@name" /> </fruit>
<xsl:text>
Shop location: </xsl:text>
<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"?> Example of
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
Named
<xsl:template match="/">
<html> Template 3
<body>
<h3>Team Members</h3>
XML
<ul> <?xml version="1.0"?>
<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
</leader>
<xsl:text>, ic = </xsl:text> <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" />
Example
<?xml version="1.0"?>
XSL <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"/> XML <member ic="106230133">
<xsl:template match="/"> <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> Output (HTML)
</xsl:template>
</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">
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'])"/>
<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>
</xsl:template> Output (HTML) <salary>4500</salary>
</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;
}