XSLT Transformations

XSLT Transformations

XSLT is a powerful language for manipulating the data in XML documents. For example, using an XSLT stylesheet, Ill be able to take the data in planets.xml and format that data into an HTML table. Stylesheets contain the rules youve set up to transform an XML document, and much of this book focuses on writing stylesheets and helping you understand how they work. Heres what the XSLT stylesheet planets.xsl, that transforms the data in planets.xml into an HTML table, looks like (well 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 thats exactly what it is. All XSLT stylesheets are also XML documents, and as such should be well- formed XML. Youll see these two documentsplanets.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 well 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 youre using an XSLT stylesheet. (To use the other type of stylesheets, cascading stylesheets [CSS]which are usually used with HTMLyoud 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.
graphics/01fig02.gif

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. Thats the first example of an XSLT transformation.

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



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

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