XSLT

Another specification incorporated into the JAXP API is the XML Stylesheet Transformation (XSLT) system. An XSLT transformation takes an input XML document and transforms it into an output format (not necessarily XML) according to a set of rules specified in an XSL stylesheet. One common application of XSL stylesheets is transforming XML into HTML for presentation in a browser; this is often done in the browser directly or on the web server via a content management system such as the Apache project’s Cocoon (http://cocoon.apache.org).

The following XSL document converts the orders.xml file from the SAX example into HTML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
     <xsl:template match="orders">
       <html>
          <head><title>Order Summary</title></head>
          <body>
          <xsl:apply-templates/>
          </body>
     </html>
     </xsl:template>
     <xsl:template match="order">
       <h1>Order Number  <xsl:value-of select="@idnumber"/></h1>
       Ship To:
     <pre>
       <xsl:value-of select="shippingaddr"/>
     </pre>
     <ul>
       <xsl:apply-templates select="item"/>
     </ul>
     </xsl:template>
     <xsl:template match="item">
           <li><xsl:value-of select="@quantity"/> of item
            <xsl:value-of select="@idnumber"/><xsl:apply-templates/></li>
     </xsl:template>
     <xsl:template match="handling">
      <br/>Special Instructions: <xsl:value-of select="."/>
     </xsl:template>
</xsl:stylesheet>

The XSL file consists of a series of templates. The XSL processor matches each ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.