- Chapter 1: Essential XSLT
- A Little Background
- XML Documents
- What Does XML Look Like in a Browser?
- XSLT Transformations
- Making an XSLT Transformation Happen
- Using Standalone XSLT Processors
- Using Browsers to Transform XML Documents
- Using XSLT and JavaScript in the Internet Explorer
- XSLT Transformations on Web Servers
- XML-to-XHTML Transformations
- XSLT Resources
- XSL Formatting Objects: XSL-FO
- XSL-FO Resources
- Formatting an XML Document
- The XSLT Stylesheet
- Transforming a Document into FormattingObject Form
- Creating a Formatted Document
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 documentand 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 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 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 HTMLyou'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?