XML-to-XHTML Transformations

XML-to-XHTML Transformations

Although many books concentrate on XML-to-HTML transformations, the truth is that the W3C isnt overwhelmingly happy about that. Theyve been trying to phase out HTML (and theyre 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, its not in widespread use yet. For that reason, Ill stick to HTML in this book, but because the W3C says you should use XHTML, Ill 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 www.w3.org/TR/xhtml1/, as well as the XHTML 1.1 recommendation at www.w3.org/TR/xhtml11/.

Although the W3C says you should be converting from XML to XHTML rather than HTML, Ive 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 thats coming.

Ill take a closer look at this type of transformation in Chapter 6, but heres 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> 

Ill 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 graphics/ccc.gif com.jclark.xsl.sax.Driver planets.xml planets.xsl planets.html 

Heres 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>1 days</td>                  <td>2107 miles</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, youve gotten a good overview of how XSLT works at this point, performing XML-to-HTML, XML, and XHTML transformations. Youll 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, theres a lot more material available to you on XSLT that you should know about, and well now take a look at what kinds of XSLT resources you can find online.



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