XSLT - Making XSLT Your Friend
What and Why
XSLT enables you to convert XML into other forms.
For example, XML markup can be converted to HTML markup for display on the web. Or, XML markup can be converted to a simple text-only Read Me file. Yet another example would be the conversion of XML into FO (Formatting Objects) for PDF production or into help systems such as JavaHelp, Eclipse Help, Microsoft HTML Help 1.x, or WebWorks Help.
10,000ft View
XSLT gives you two basic building blocks:
- Templates
- Matching system
Templates
Templates enable you to emit markup.
<stylesheet> <template> ... </template> <template> ... </template> </stylesheet>
For example, given this input:
<topic> <title>Power Hour</title> </topic>
XSLT enables you to output a simple HTML document using this XML source:
<xsl:template ...> <html> <head> <title>Power Hour</title> </head> <body> <h2>Power Hour</h2> </body> </html> </xsl:template>
Matching system
XSLT's matching system enables you to say when/where a template should be used.
<stylesheet> <template match="<XPath expressions>"> ... </template> <template match="<XPath expressions>"> ... </template> </stylesheet>
Using the XML example above:
For example, given this input:
<topic> <title>Power Hour</title> </topic>
XSLT allows us to perform different actions in different situations:
<xsl:template match="topic"> <html> <head> <title>Power Hour</title> </head> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="title"> <h2>Power Hour</h2> </xsl:template>
Result:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Power Hour</title> </head> <body> <h2>Power Hour</h2> </body> </html>
XSLT Transform Playground
You can experiment with XSLT in your browser with WebWorks' "XSLT Transforms" toy. Access it from:
Clicking on the link for "XSLT Transforms" creates a new session for you:
You can create a bookmark to return to this session at any time in the future. The XSLT toy will automatically save and apply changes you make to your XML input and XSLT stylesheet as you type.
Power Hour XML + XSLT
During the Power Hour session, we created a simple XML input document and XSLT transform to display the XML as HTML. We used the following XML input and XSLT stylesheet:
- XML Input.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <topic> 3 <title>Power Hour - Is a place to learn about XSLT</title> 4 <body> 5 <p>Learning about XSLT today.</p> 6 <p>More text</p> 7 <p>Still more text</p> 8 <p>Learning about XSLT today.</p> 9 <p>More text</p> 10 <p>Still more text</p> 11 <p>Learning about XSLT today.</p> 12 <p>More text</p> 13 <p>Still more text</p> 14 <p>Learning about XSLT today.</p> 15 <p>More text</p> 16 <p>Still more text</p> 17 <p>Learning about XSLT today.</p> 18 <p>More text</p> 19 <p>Still more text</p> 20 </body> 21 </topic>
- XSLT Stylesheet.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:html="http://www.w3.org/1999/xhtml" 4 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 5 exclude-result-prefixes="html xsl" 6 > 7 <xsl:namespace-alias stylesheet-prefix="html" result-prefix="#default" /> 8 9 <xsl:output method="html" version="1.0" encoding="utf-8" indent="yes" /> 10 11 <xsl:template match="/"> 12 <html> 13 <xsl:apply-templates mode="head" /> 14 <xsl:apply-templates /> 15 </html> 16 </xsl:template> 17 18 <xsl:template match="topic" mode="head"> 19 <head> 20 <xsl:apply-templates mode="head" /> 21 </head> 22 </xsl:template> 23 24 <xsl:template match="title" mode="head"> 25 <title> 26 <xsl:value-of select="./text()" /> 27 </title> 28 </xsl:template> 29 30 <xsl:template match="text()" mode="head"> 31 <!-- Ignore it --> 32 </xsl:template> 33 34 <xsl:template match="topic"> 35 <xsl:apply-templates /> 36 </xsl:template> 37 38 <xsl:template match="body"> 39 <body> 40 <h2><xsl:value-of select="preceding-sibling::title/text()" /></h2> 41 42 <xsl:apply-templates /> 43 </body> 44 </xsl:template> 45 46 <xsl:template match="p"> 47 <div> 48 <xsl:value-of select="./text()" /> 49 </div> 50 </xsl:template> 51 52 <xsl:template match="text()"> 53 <!-- Ignore --> 54 </xsl:template> 55 </xsl:stylesheet>
You can experiment with this example by creating a new "XSLT Transforms" toy and cut/pasting contents into the appropriate input area.
Terminology
- Stylesheet
- XSLT source file which contains your Templates.
- Templates
- Markup to emit when a particular element/node is matched.
- XPath Expression
- Way to express how content should be matched. Enables you to treat an XML document like a database and perform queries against it.