XSLT

HomePage | RecentChanges | Preferences

Transforming XML into HTML using XSLT

Produce the XML:

      :SELECT PERSON SAMPLE 2                                                         

      2 records selected to list 0.                                                   
                                                                                
      >DOWNLOAD PERSON @ID PERSON.NAME FORMAT XML                                     
       DOWNLOAD version 7.01                                                           
       Using input file PERSON                                                         
       Output to screen                                                                
                                                                                
       <?xml version="1.0"?>                                                           
       <download>                                                                      
       <person>                                                                        
         <id>0585091</id>                                                              
         <personname>Mrs. Wilma Flintstone</personname>                                
       </person>                                                                       
       <person>                                                                        
         <id>0586473</id>                                                              
         <personname>Ms. Betty Rubble</personname>                                     
       </person>                                                                       
       </download>   

Write the XSL Stylesheet:

     <?xml version="1.0" encoding="ISO-8859-1"?>
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="download">
   
   
   
</xsl:template> <xsl:template match="person"> <tr> <td> <a href="http://www.example.com/viewProfile.do?selectedId={id}" target="profile"> <xsl:value-of select="id" /> </a> </td> <td> <xsl:value-of select="personname" /> </td> </tr> </xsl:template> </xsl:stylesheet>

Apply the Stylesheet to the XML:

The easiest way to do this is to add the following line at the top of the XML file: <?xml-stylesheet type="text/xsl" href="person.xsl"?>

It can also be done with JSTL tags in a JSP if you're running a Servlet container such as Tomcat 4.1

The Result:

Assuming the xml file above was saved as 'person.xml' and the stylesheet as 'person.xsl' and they both live in the same directory, pull up person.xml in your browser, and you should see an HTML table with a border containing:

-------------------------------------------
| 0585091    |  Mrs. Wilma Flintstone      |
| 0586473    |  Ms. Betty Rubble           |
------------------------------------------- 

If you would rather produce PDF instead of HTML, take a look at Apache FOP

The author can be reached at wsmoak -at- apache.org


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited November 19, 2005 2:26 pm by WendySmoak (diff)
Search: