WebApp: XML-based Web Application
1
XSLT (XML Stylesheet Language Transformation)
XSLT (XML Stylesheet Language
Transformation)
This module will discuss another XML-related technology; XSLT. Just like
XPath, it can also extend the function of XML. XSLT is mainly used for
transforming XML documents. This module covers the basics of XSLT and
how it transforms an XML document.
Upon completion of this module, the students should be able to:
1. Define what is XSLT
2. Discuss the purpose of XSLT
3. Identify the syntax of XSLT
What is XSLT?
XSLT Defined
XSLT stands for eXtended Stylesheet Transformation. XSLT is a language
used to transform XML documents into other XML documents, XHTML file, or
any other XML document formats.
Below is an Example of an XML Document
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<cinema>
<movie genre="horror">
<title lang="en">House in the Woods</title>
<director>Dan Morgan</director>
<year>2007</year>
</movie>
<movie genre="action">
<title lang="en">A time to Fight</title>
<director>Jake Johnson</director>
<year>1966</year>
</movie>
<movie genre="comedy">
<title lang="en">The adventures of Earl</title>
<director>Sam Clover</director>
Course Module
<year>1975</year>
<price>49.99</price>
</movie>
<movie genre="drama">
<title lang="en">She loves me</title>
<director>Erik T. Ray</director>
<year>2003</year>
<price>39.95</price>
</movie>
</cinema>
The output of the XML document above is:
If we apply the XSLT Below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Cinema</h2>
<table border="1">
WebApp: XML-based Web Application
3
XSLT (XML Stylesheet Language Transformation)
<tr bgcolor="cyan">
<th style="text-align:left">Movie Title</th>
<th style="text-align:left">Director</th>
</tr>
<xsl:for-each select="cinema/movie">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="director"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output is:
XPath and XSLT
XPath is use in XSLT to select the parts of an XML document.
Namespace and XSLT
XSLT uses namespace on its document.
W3C Recommended
XSLT is W3C Recommended.
Course Module
Purpose of XSLT
XSLT uses XPath to navigate through XML documents and to define parts of
the source document that should match one or more predefined templates.
XSLT will transform the matching part of the source document into the result
document when a match is found.
XSLT has its own processor. An XSLT stylesheet is applied on XML documents
to produce a new format.
Figure 1: XSLT transformation Process
Source: http://www.techrepublic.com/article/introduction-to-xslt/
XSLT Syntax
Declaration
In declaring XSLT documents, we can use the
<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">
These two ways of declaring an XSLT documents are interchangeable,
meaning you can use any of the two.
Elements and attributes
1. The <xsl:template> element/tag
This element is used to build templates.
2. The <xsl:value-of> element/tag
This element can be used to extract the value of an XML element and add it to
the output stream of the transformation
3. The match attribute
This 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.
4. The select attribute
This contains an XPath expression.
5. The <xsl:for-each> element/tag
This element can be used to select every XML element of a specified node-set
WebApp: XML-based Web Application
5
XSLT (XML Stylesheet Language Transformation)
6. The <xsl:sort> element tag
This element is used to sort the output.
7. The <xsl:if> element/tag
This element is used to put a conditional test against the content of the XML
file.
8. The <xsl:choose> element/tag
This element is used in conjunction with <xsl:when> and
<xsl:otherwise> to express multiple conditional tests.
More examples about the syntax of XSLT are discussed on Video007.
Glossary
XSLT: This stands for eXtended Stylesheet Transformation. XSLT is a
language used to transform XML documents into other XML documents
References and Supplementary Materials
Books and Journals
Goldberg, K. H. ;2010; XML Visual Quick Start Guide .2nd Edition; United
States of America; Peachpit
Press.Nguyen, V. ;2017; Using XML. United States of America; Amazon Digital
Services LLC.
Online Supplementary Reading Materials
XSL Intro; https://www.w3schools.com/xml/xsl_intro.asp; Accessed on
8/7/2017
XSL Languages; https://www.w3schools.com/xml/xsl_languages.asp;
Accessed on 8/7/2017
XSL Transformation;
https://www.w3schools.com/xml/xsl_transformation.asp; Accessed on
8/7/2017
XSL Template; https://www.w3schools.com/xml/xsl_templates.asp;
Accessed on 8/7/2017
XSL Value; https://www.w3schools.com/xml/xsl_value_of.asp; Accessed on
8/7/2017
XSL For Each; https://www.w3schools.com/xml/xsl_for_each.asp; Accessed
on 8/7/2017
XSL Sort; https://www.w3schools.com/xml/xsl_sort.asp; Accessed on
8/7/2017
Course Module
Instructional Videos
XSL Introduction; https://www.youtube.com/watch?v=RsRPDqgfL1E;
Accessed on 8/7/2017