Table Data to PDF in Java using XML / XSLT

In the previous tutorial, we described how to convert SQL data stored in a database table to PDF file using the Java iText library. The example tutorial ended up producing the PDF file we wanted, however, the table data was stamped as full XML inside the PDF document. Normally, you would like to see the data in a tabular format. 

In this tutorial, we will provide a simple stylesheet (XSLT) that you can use to convert the XML generated out of SQL data into a flat table. Readers are strongly recommended to read the original tutorial as this is an extension of that. Read it here below:

The XSLT that will do the conversion for us is provided below:

 1 <?xml version="1.0" encoding="ISO-8859-1"?>
 2 <xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:os="">
 3     <!-- <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/> -->
 4     <xsl:template match="/">
 5         <html>
 6             <body>
 7               <table>
 8                 <xsl:for-each select="//os:currentRow">  
 9                 <tr>
10                 <td><xsl:value-of select="os:columnValue[1]"/></td>
11                 <td><xsl:value-of select="os:columnValue[2]"/></td>
12                 <td><xsl:value-of select="os:columnValue[3]"/></td>
13                 <td><xsl:value-of select="os:columnValue[4]"/></td>
14                 </tr>
15                 </xsl:for-each>  
16                 </table>
17             </body>
18         </html>
19     </xsl:template>
20 </xsl:stylesheet>>
Once you have this XSLT (you can convert this to any format), you can use the tutorial that we provided earlier to convert the XML generated out of the SQL to a table. Refer to the tutorial provided earlier, that explained how to convert XML to PDF with XSLT.

Still wondering how to complete this, wait for the complete code example to follow.

No comments:

Post a Comment