Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

XSLT Transformations

XSLT is a powerful language for manipulating the data in XML documents. For example, using an XSLT stylesheet, I'll be able to take the data inplanets.xml and format that data into an HTML table. Stylesheets contain the rules you've set up to transform an XML document, and much of this book focuses on writing stylesheets and helping you understand how they work. Here's what the XSLT stylesheet planets.xsl, that transforms the data inplanets.xml into an HTML table, looks like (we'll dissect it in Chapter 2):

Listing 1.2 planets.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <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>

You can see that this XSLT stylesheet has the look of an XML document—and for good reason, because that's exactly what it is. All XSLT stylesheets are also XML documents, and as such should be well-formed XML. You'll see these two documents—planets.xml (as given in Listing 1.1) and its associated stylesheet, planets.xsl (as given in Listing 1.2)—throughout the book as we perform XSLT transformations in many different ways.

How do you connect this stylesheet to the XML document planets.xml? As we'll see in the next chapter, one way to do that is with an <?xml-stylesheet?> XML processing instruction. This processing instruction uses two attributes. The first attribute is type, which you set to "text/xml" to indicate that you're using an XSLT stylesheet. (To use the other type of stylesheets, cascading stylesheets [CSS]—which are usually used with HTML—you'd use "text/css".) The second attribute is href, which you set to the URI (recall that XML uses Uniform Resource Identifiers, URIs, rather than URLs) of the stylesheet:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="planets.xsl"?>
<PLANETS>

  <PLANET>
    <NAME>Mercury</NAME>
    <MASS UNITS="(Earth = 1)">.0553</MASS>
    <DAY UNITS="days">58.65</DAY>
    <RADIUS UNITS="miles">1516</RADIUS>
    <DENSITY UNITS="(Earth = 1)">.983</DENSITY>
    <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->
  </PLANET>
    .
    .
    .

Now I can use an XSLT processor to apply planets.xsl to planets.xml and create a new document, planets.html. The XSLT processor creates planets.html, and you can see that new HTML document in Figure 1.2.

Figure 1.2 An HTML document created by an XSLT processor.

As you see in Figure 1.2, the XSLT processor read the data in planets.xml, applied the rules put into planets.xsl, and created an HTML table in planets.html. That's the first example of an XSLT transformation.

What actually happened here? You've seen the XML document, planets.xml, and the XSLT stylesheet, planets.xsl. But how did they combine to create planets.html?

  • + Share This
  • 🔖 Save To Your Account