XSLT
XSLT
Reference:
www.cis.upenn.edu/~matuszek/ cit597-
2002/Lectures/xslt.ppt
XSLT
• XSLT stands for Extensible Stylesheet Language
Transformations
• XSLT is used to transform XML documents into
other kinds of documents--usually, but not
necessarily, XHTML
• XSLT uses two input files:
– The XML document containing the actual data
– The XSL document containing both the “framework” in
which to insert the data, and XSLT commands to do so
Very simple example
• File data.xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="render.xsl"?>
<message>Howdy!</message>
• File render.xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- one rule, to transform the input root (/) -->
<xsl:template match="/">
<html><body>
<h1><xsl:value-of select="message"/></h1>
</body></html>
</xsl:template>
</xsl:stylesheet>
The .xsl file
• An XSLT document has the .xsl extension
• The XSLT document begins with:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/
XSL/Transform">
• Contains one or more templates, such as:
<xsl:template match="/"> ... </xsl:template>
• And ends with:
</xsl:stylesheet>
Finding the message text
• The template <xsl:template match="/"> says to
select the entire file
– You can think of this as selecting the root node of the
XML tree
• Inside this template,
– <xsl:value-of select="message"/> selects the
message child
– Alternative Xpath expressions that would also work:
• ./message
• /message/text() (text() is an XPath function)
• ./message/text()
Putting it together
• The XSL was:
<xsl:template match="/">
<html><body>
<h1><xsl:value-of select="message"/></h1>
</body></html>
</xsl:template>
• <xsl:template match="tableOfContents">
<h1>Table of Contents</h1>
<xsl:apply-templates select="chapterNumber"/>
<xsl:apply-templates select="chapterName"/>
<xsl:apply-templates select="pageNumber"/>
</xsl:template>
• Etc.
xsl:apply-templates
• The <xsl:apply-templates> element applies a
template rule to the current element or to the
current element’s child nodes
• If we add a select attribute, it applies the template
rule only to the child that matches
• If we have multiple <xsl:apply-templates>
elements with select attributes, the child nodes
are processed in the same order as the <xsl:apply-
templates> elements
When templates are ignored
• Templates aren’t used unless they are applied
– Exception: Processing always starts with select="/"
– If it didn’t, nothing would ever happen
• If your templates are ignored, you probably forgot
to apply them
• If you apply a template to an element that has
child elements, templates are not automatically
applied to those child elements
Applying templates to children
• <book>
<title>XML</title> With this line:
<author>Gregory Brill</author> XML by Gregory Brill
</book>
• <xsl:template match="/">
<html> <head></head> <body>
<b><xsl:value-of select="/book/title"/></b>
<xsl:apply-templates select="/book/author"/>
</body> </html>
</xsl:template>
<xsl:template match="/book/author">
by <i><xsl:value-of select="."/></i> Without this line:
</xsl:template> XML
Calling named templates
• You can name a template, then call it, similar to the way you
would call a method in Java
• The named template:
<xsl:template name="myTemplateName">
...body of template...
</xsl:template>
• A call to the template:
<xsl:call-template name="myTemplateName"/>
• Or:
<xsl:call-template name="myTemplateName">
...parameters...
</xsl:call-template>
Templates with parameters
• Parameters, if present, are included in the content of
xsl:template, but are the only content of xsl:call-
template Single quotes inside double
• Example call: quotes make this a string
<xsl:call-template name="doOneType">
<xsl:with-param name="header" select="'Lectures'"/>
<xsl:with-param name="nodes" select="//lecture"/>
</xsl:call-template>
• Example template:
This parameter is a
<xsl:template name="doOneType">
typical XPath expression
<xsl:param name="header"/>
<xsl:param name="nodes"/>
...body of template...
</xsl:template>
• Parameters are matched up by name, not by position
Thoughts on XSL
• XSL is a programming language--and not a particularly
simple one
– Expect to spend considerable time debugging your XSL
• These slides have been an introduction to XSL and
XSLT--there’s a lot more of it we haven’t covered
• As with any programming, it’s a good idea to start simple
and build it up incrementally: “Write a little, test a little”
– This is especially a good idea for XSLT, because you don’t get a lot
of feedback about what went wrong
• I use jEdit with the XML plugin
– I find it to be a big help, expecially with XML syntax
– My approach is: write (or change) a line or two, check for syntax
errors, then jump to IE and reload the XML file
The End