Abbey Workshop

XSLT Sample Templates

This tip provides of a couple examples of simple XSLT stylesheets. Both templates are designed to tranform the student_directory.xml file. The XML file represent a student directory from a fictional college. The first stylesheet converts the file into text, the second converts the file into an HTML page.

Listing for: student_directory.xml

   1 <student_list>
   2   <student>
   3     <name>George Washington</name>
   4     <major>Politics</major>
   5     <phone>312-123-4567</phone>
   6     <email>gw@example.edu</email>
   7   </student>
   8   <student>
   9     <name>Janet Jones</name>
  10     <major>Undeclared</major>
  11     <phone>311-122-2233</phone>
  12     <email>janetj@example.edu</email>
  13   </student>
  14   <student>
  15     <name>Joe Taylor</name>
  16     <major>Engineering</major>
  17     <phone>211-111-2333</phone>
  18     <email>joe@example.edu</email>
  19   </student>
  20 </student_list>

XSLT Text Stylesheet

This stylesheet converts the XML file into a text file. The default output method of XSLT is XML. Therefore, the xsl:output tag is used to tell the processor to output to text.

Listing for: student_text.xsl

   1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   2 <xsl:output method="text"/>
   3 
   4 <xsl:template match="/">
   5   <xsl:apply-templates />
   6 </xsl:template>
   7 
   8 <xsl:template match="student_list">
   9   Student Directory for example.edu
  10   <xsl:apply-templates />
  11 </xsl:template>
  12 
  13 <xsl:template match="name">
  14   Name: <xsl:apply-templates />
  15 </xsl:template>
  16 
  17 <xsl:template match="major">
  18   Major: <xsl:apply-templates />
  19 </xsl:template>
  20 
  21 <xsl:template match="phone">
  22   Phone: <xsl:apply-templates />
  23 </xsl:template>
  24 
  25 <xsl:template match="email">
  26   Email: <xsl:apply-templates />
  27 </xsl:template>
  28 
  29 </xsl:stylesheet>

Here is the output from the transformation.

View: studentout.txt

XSLT HTML Stylesheet

This stylesheet converts the XML file into an HTML file. Note that any HTML must be well formed to be stored in the an XSLT stylesheet. Most transformation engines will automatically remove trailing slashes to make the HTML work better with the browser.

Listing for: student_html.xsl

   1 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   2 <xsl:output method="html"/>
   3 
   4 <xsl:template match="/">
   5 <html>
   6   <head>
   7     <title>Student Directory</title>
   8   </head>
   9   <body>
  10   <xsl:apply-templates />
  11   </body>
  12 </html>
  13 </xsl:template>
  14 
  15 <xsl:template match="student_list">
  16   <h3>Student Directory for example.edu</h3>
  17   <xsl:apply-templates />
  18 </xsl:template>
  19 
  20 <xsl:template match="name">
  21   <p>Name: <xsl:apply-templates /></p>
  22 </xsl:template>
  23 
  24 <xsl:template match="major">
  25   <p>Major: <xsl:apply-templates /></p>
  26 </xsl:template>
  27 
  28 <xsl:template match="phone">
  29   <p>Phone: <xsl:apply-templates /></p>
  30 </xsl:template>
  31 
  32 <xsl:template match="email">
  33   <p>Email: <xsl:apply-templates /></p>
  34 </xsl:template>
  35 
  36 </xsl:stylesheet>

Here is the HTML file produced from the transformation.

Listing for: student.html

   1 <html>
   2 <head>
   3 <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
   4 <title>Student Directory</title>
   5 </head>
   6 <body>
   7 <h3>Student Directory for example.edu</h3>
   8   
   9     
  10 <p>Name: George Washington</p>
  11     
  12 <p>Major: Politics</p>
  13     
  14 <p>Phone: 312-123-4567</p>
  15     
  16 <p>Email: gw@example.edu</p>
  17   
  18   
  19     
  20 <p>Name: Janet Jones</p>
  21     
  22 <p>Major: Undeclared</p>
  23     
  24 <p>Phone: 311-122-2233</p>
  25     
  26 <p>Email: janetj@example.edu</p>
  27   
  28   
  29     
  30 <p>Name: Joe Taylor</p>
  31     
  32 <p>Major: Engineering</p>
  33     
  34 <p>Phone: 211-111-2333</p>
  35     
  36 <p>Email: joe@example.edu</p>
  37   
  38 
  39 </body>
  40 </html>