Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XSL gives your XML some style

Use XSL and servlets to style your XML data

  • Print
  • Feedback

Page 5 of 7

Moving on, let's look at the XML schema for our transactions:

<!ELEMENT orders (invoice)+>
<!ELEMENT invoice (item+,name,address+)>
<!ELEMENT item EMPTY>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (street, city, state, zip)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ATTLIST invoice
  number        CDATA #REQUIRED
  orderDate     CDATA #REQUIRED
  shipDate      CDATA #REQUIRED>
<!ATTLIST item
  part          CDATA #REQUIRED
  quantity      CDATA #REQUIRED
  cost          CDATA #REQUIRED>
<!ATTLIST address
  type          (shipping|billing|other) #REQUIRED>


Note: XSL doesn't require a DTD to do its processing -- a good feature. Therefore, if minor changes occur in the schema, the same stylesheets can still be used.

In our example, four stylesheets will be created, each representing a different view:

  1. Transaction view: A list of each day's transactions, including invoice number, customer name, and invoice total.
  2. Products sold view: A list of all the products sold for each day, including the quantity.
  3. Detail view: A drill-down from the Transaction and Products sold views. When the user clicks the invoice number, the details for the transaction display.
  4. XML view: A display of the XML we are processing against.


Those examples will lack spinning logos, thus demonstrating the power of XSL without cluttering the output with graphics.

The HTML that calls the servlet will use frames. The left frame will have a list of the stylesheets available. When a stylesheet is selected, a request will be sent to the servlet. Then the servlet will open the XML and the stylesheet, and use the XSLProcessor to do the translation. The translation will be in the form of HTML, which will be pushed out to the browser.

Let's go over the Transaction view, which shows the list of invoices, then the Detail view, which shows the details of the invoice.

The Transaction view

Here's the Transaction view -- a list of the day's invoices:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:java="http://xml.apache.org/xslt/java"
                exclude-result-prefixes="java"
                version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/orders">
  <html>
    <h1>Today's Invoices</h1>
    <body>
      <table border="on" cellspacing="1" cellpadding="5">
      <thead>
      <th>Invoice Number</th>
      <th>Customer Name</th>
      <th>Total Purchase</th>
      </thead>
      <tbody>
      <xsl:for-each select="invoice">
      <tr>
      <td>
      <xsl:element name="a">
        <xsl:attribute 
name="href"><xsl:text>xsl?stylesheet=detail&invoiceNum=</xsl:text><xsl:value-of 
select="@number"/></xsl:attribute>
        <xsl:value-of select="@number"/>
      </xsl:element>
      </td>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of 
select="java:SumInvoice.summ(item)"/></td>
      </tr>
      </xsl:for-each>
      </tbody>
      </table>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>


Figure 1 shows the results.

Figure 1. Stylesheet 1: Output from the Transaction view (40 KB)



The Detail view

Now we turn our attention to the Detail view:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:java="http://xml.apache.org/xslt/java"
                exclude-result-prefixes="java"
                version="1.0">
<xsl:param name="invoiceNum" select="string('none')"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
  <body>
     <table border="on" cellspacing="1" cellpadding="5">
     <xsl:for-each select="orders/invoice[@number=$invoiceNum]">
       <tr>
         <td>
           <table border="on" cellspacing="1" cellpadding="5">
             <tr><td><b>Invoice 
Number:</b><xsl:value-of select="@number"/></td><td 
colspan="3"><xsl:value-of select="name"/></td></tr>
             <tr>
               <td colspan="4">
               <xsl:apply-templates select="address"/>
               </td>
             </tr>
             <tr><td 
colspan="4"><b>Items</b></td></tr>
             
<tr><td>Part</td><td>Quantity</td><td>Cost</td><td>Subtotal</td></tr>
             <xsl:for-each select="item">
               <tr>
                 <td><xsl:value-of select="@part"/></td>
                 <td><xsl:value-of 
select="@quantity"/></td>
                 <td><xsl:value-of select="@cost"/></td>
                 <td><xsl:value-of select="@quantity * 
@cost"/></td>
               </tr>
               <xsl:if test="last()">
               </xsl:if>
             </xsl:for-each>
               <tr>
                 
<td><b><xsl:text>Total</xsl:text></b></td>
                 <td><xsl:value-of 
select="sum(item/@quantity)"/></td>
                 <td><xsl:text></xsl:text></td>
                 <td>
                   <xsl:value-of select="java:SumInvoice.summ(item)"/>
                 </td>
               </tr>
           </table>
         </td>
       </tr>
     </xsl:for-each>
     </table>
  </body>
</html>
</xsl:template>
<xsl:template match="address">
<table>
  <tr><td><i>
    <xsl:choose>
      <xsl:when test="@type='shipping'">
        <xsl:text>Shipping Address</xsl:text>
      </xsl:when>
      <xsl:when test="@type='billing'">
        <xsl:text>Billing Address</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>Other Address</xsl:text>
      </xsl:otherwise>
    </xsl:choose></i>
  </td></tr>
  <tr><td colspan="4"><xsl:value-of 
select="street"/></td></tr>
  <tr><td colspan="4"><xsl:value-of 
select="city"/><xsl:text> </xsl:text><xsl:value-of 
select="state"/>, <xsl:value-of 
select="zip"/></td></tr>
</table>
</xsl:template>
</xsl:stylesheet>


The results can be seen in Figure 2.

  • Print
  • Feedback

Resources