Working with XSLT

We've already been using Extensible Stylesheet Language Transformations (XSLT) throughout the book to show how XPath works, and in this chapter, we're going to take a closer look at XSLT. After all, XSLT is the major reason most people who use XPath 1.0 use XPath in the first place.

XSLT itself is really part of a larger specification, Extensible Stylesheet Language (XSL). XSLT is the most popular part of XSL, because it lets you work with the data in an XML document and transform it into other formats, including HTML or just plain text. You can also use XSLT to transform XML documents into other XML documents where you've arranged the data in a different way.

XSLT is an W3C specification and has been a recommendation since November 16, 1999. You can find the W3C recommendation for XSLT 1.0, the current version, at www.w3.org/TR/xslt. XSLT 2.0 is in the works, but it's only a working draft at this point (there actually was an XSLT 1.1, but it was not continued after the working draft stage). You can see the current version of the XSLT 2.0 Working Draft at http://www.w3.org/TR/xslt20/.

To use XSLT, you need two documentsan XML document that you want to transform, and an XSLT stylesheet that will direct how the transformation works (note that XSLT stylesheets are also XML documents). And you'll also need an XSLT processor.

We took a look at this process in Chapter 1, but we'll see it in more depth now. We'll start this chapter with our planetary data document, renamed ch05_01.xml so we can use it in this chapter, and an XSLT stylesheet that we first saw in Chapter 1, which you see in ch05_02.xsl in Listing 5.1.

Listing 5.1 Transforming XML Data into HTML ( ch05_02.xsl )
 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <!-- This template matches all planets elements -->     <xsl:template match="/planets">         <HTML>             <HEAD>                 <TITLE>                     The Planets Table                 </TITLE>             </HEAD>             <BODY>                 <H1>                     The Planets Table                 </H1>                 <TABLE BORDER="2">                     <TR>                         <TD>Name</TD>                         <TD>Mass</TD>                         <TD>Radius</TD>                         <TD>Day</TD>                     </TR>                     <xsl:apply-templates/>                 </TABLE>             </BODY>         </HTML>     </xsl:template>     <xsl:template match="planet">        <TR>           <TD><xsl:value-of select="name"/></TD>           <TD><xsl:apply-templates select="mass"/></TD>           <TD><xsl:apply-templates select="radius"/></TD>           <TD><xsl:apply-templates select="day"/></TD>        </TR>    </xsl:template>     <xsl:template match="mass">         <xsl:value-of select="."/>         <xsl:text> </xsl:text>         <xsl:value-of select="@units"/>     </xsl:template>     <xsl:template match="radius">         <xsl:value-of select="."/>         <xsl:text> </xsl:text>         <xsl:value-of select="@units"/>     </xsl:template>     <xsl:template match="day">         <xsl:value-of select="."/>         <xsl:text> </xsl:text>         <xsl:value-of select="@units"/>     </xsl:template> </xsl:stylesheet> 

This stylesheet extracts the XML data in ch05_01.xml and formats it into an HTML table. So how do you make this transformation happen?



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net