- 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
XML-to-XHTML Transformations
Although many books concentrate on XML-to-HTML transformations, the truth is that the W3C isn't overwhelmingly happy about that. They've been trying to phase out HTML (and they're the ones who standardized it originally) in favor of their new specification, XHTML, which is an XML-compliant revision of HTML. XHTML documents are also well-formed valid XML documents, so transforming from XML to XHTML is really transforming from XML to a special kind of XML.
Although the W3C is really pushing XHTML, it's not in widespread use yet. For that reason, I'll stick to HTML in this book, but because the W3C says you should use XHTML, I'll take a brief look at that here and in Chapter 6. If you want to learn more about XHTML, take a look at the W3C XHTML 1.0 recommendation at http://www.w3.org/TR/xhtml1/, as well as the XHTML 1.1 recommendation at http://www.w3.org/TR/xhtml11/.
Although the W3C says you should be converting from XML to XHTML rather than HTML, I've never seen a working example on the W3C site. The examples that they do present do not, in fact, produce valid XHTML documents. However, support for XML-to-XHTML transformations is supposed to be built into XSLT 2.0, so presumably that's coming.
I'll take a closer look at this type of transformation in Chapter 6, but here's a working version of planets.xsl that will create a valid XHTML version of planets.html. Note that this time you need to use the doctype-public attribute in the <xsl:output> element, and although that is correct XSLT, not all XSLT processors can handle it:
Listing 1.7 XML-to-XHTML Transformation
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" doctype-system ="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/> <xsl:template match="/PLANETS"> <html> <head> <title> The Planets Table </title> </head> <body> <h1> The Planets Table </h1> <table> <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>
I'll convert planets.xml into a valid XHTML document, planets.html, using this new version of planets.xsl and the XT XSLT processor. First, I set the classpath as needed:
C:\>set classpath=c:xerces\xerces-1_3_0\xerces.jar;c:\xt\xt.jar;
Then I perform the transformation:
C:\planets>java - Dcom.jclark.xsl.sax.parser=org.apache.xerces.parsers.SAXParser _com.jclark.xsl.sax.Driver planets.xml planets.xsl planets.html
Here's the resulting XHTML file, planets.html:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title> The Planets Table </title> </head> <body> <h1> The Planets Table </h1> <table> <tr> <td>Name</td> <td>Mass</td> <td>Radius</td> <td>Day</td> </tr> <tr> <td>Mercury</td> <td>.0553 (Earth = 1)</td> <td>1516 miles</td> <td>58.65 days</td> </tr> <tr> <td>Venus</td> <td>.815 (Earth = 1)</td> <td>3716 miles</td> <td>116.75 days</td> </tr> <tr> <td>Earth</td> <td>1 (Earth = 1)</td> <td>2107 miles</td> <td>1 days</td> </tr> </table> </body> </html>
This document, planets.html, does indeed validate as well-formed and valid transitional XHTML 1.0 (the kind of XHTML in most popular use) according to the W3C HTML and XHTML validation program. The HTML/XHTML validator tool can be found online at http://validator.w3.org/file-upload.html. Chapter 6 provides more information on XML-to-XHTML transformations.
So far, you've gotten a good overview of how XSLT works at this point, performing XML-to-HTML, XML, and XHTML transformations. You'll also see XML-to-RTF (Rich Text Format text), to plain text, to XSL-FO, to JavaScript, to SQL-based databases, as well as other types of XSLT transformations in this book. In addition, there's a lot more material available to you on XSLT that you should know about, and we'll now take a look at what kinds of XSLT resources you can find online.