SDSU Emerging Technology
Fall Semester, 2004
XSLT
Previous     Lecture Notes Index     Next     
© 2004, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 16-Sep-04

CS 683 Emerging Technologies Fall Semester, 2004 Doc 9 XSLT

Contents

References

Transforming XML - XSLT

Hello World Example

Applying Transformations

<xsl:template>

Built-in Rules

Attributes

<xsl:if>

CD Catalog Example

<xsl:for-each>

<xsl:sort>

<xsl:if>

<xsl:choose>

<xsl:number>

Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.

References

XSL Transformations (XSLT) Version 1.0

http://www.w3.org/TR/xslt

Learning XML , Erik Ray, O'Reilly, 2001

ZVON Miloslav Nic’s XSLT Tutorial, http://www.zvon.org/xxl/XSLTutorial/Output/index.html

ZVON Miloslav Nic’s & Jiri Jirat XPath Tutorial, http://www.zvon.org/xxl/XPathTutorial/General/examples.html

W3Schools XSLT Tutorial, http://www.w3schools.com/xsl/default.asp

Transforming XML - XSLT

Extensible Stylesheet Language for Transformation (XSLT)

Hello World Example

File hello.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<greetings>Hello World</greetings>

File hello.xsl

<?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="/">
   <html>
      <title>
         <xsl:value-of select="greetings"/>
      </title>
   <body>
      <h1>
         <xsl:value-of select="greetings"/>
      </h1>
   </body>
   </html>
</xsl:template>
</xsl:stylesheet>



Applying Transformations

Browser based

Place both files in the same directory

Open hello.xml with an XML/XSLT aware web browser

Program BasedVisualWorks

Load the parcel xsl.pst. The following code assumes the files are in the same directory at the image. For more information read pages 529 to 533 of the VisualWorks Developers Guide.

xslRules := ( XSL.RuleDatabase new ) readFileNamed: 'hello.xsl'. 
parser := XMLParser new validate: false. 
xmlDocument := parser parse: 'hello.xml' asFilename readStream.
transformedDocument := xslRules process: xmlDocument.

transformedDocument

<html>
   <title>Hello World</title>
   <body>
      <h1>Hello World</h1>
   </body>
</html>

Java

Download Xalan-Java ( http://xml.apache.org/xalan-j/)

Place the xalan-2.6.0.jar in your classpath

Command-line

java org.apache.xalan.xslt.Process -in hello.xml -xsl hello.xsl

Program

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
   
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
   
public class SimpleTransform
   {
   public static void main(String[] args) throws 
      TransformerException, TransformerConfigurationException, 
      FileNotFoundException, IOException
      {  
      TransformerFactory tFactory = 
         TransformerFactory.newInstance();
   
      Transformer transformer = 
         tFactory.newTransformer(new StreamSource("hello.xsl"));
   
      transformer.transform(
         new StreamSource("hello.xml"), 
         new StreamResult(new FileOutputStream("hello.html")));
      }
   }

<xsl:template>

<xsl:template> contains

Some Matches

items/item[position()>1] matches any item element that has a items parent and that is not the first item child of its parent

More Matching

Some Match Examples

Input

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
   </body></html>
</xsl:template>
<html>
        <body></body>
</html>
   

XSLT

Result

<xsl:template match="/">
   <html>
      <xsl:value-of select="//d" />
   </html>
</xsl:template>
<html>
        <body>Your Dog</body>
</html>
   

Which Dog

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
   <xsl:value-of select="/a/d" />
   </body></html>
</xsl:template>
<html>
        <body>Your Dog</body>
</html>

XSLT

Result

<xsl:template match="/">
   <html><body>
   <xsl:value-of select="/a/c/d" />
   </body></html>
</xsl:template>
<html>
        <body>My Dog</body>
</html>

XSLT

Result

<xsl:template match="/">
   <html>
      <xsl:value-of select="d" />
   </html>
</xsl:template>
<html></html>

Input

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   </body></html>
</xsl:template>
   
 <xsl:template match="b">
   <h1>
      <xsl:apply-templates />
   </h1>
</xsl:template>
   
 <xsl:template match="d">
   <b>
      <xsl:apply-templates />
   </b>
</xsl:template>
<html>
        <body>
   
                <h1>My Cat</h1>
        
                <h1>B2</h1>
        
                <b>Your Dog</b>
        
                
                <b>My Dog</b>
                
        
   
        </body>
</html>

Both <xsl:apply-templates> and <xsl:value-of > are recursive

Just The dogs?

Input

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   <body></html>
</xsl:template>
   
 <xsl:template match="d">
   <b>
      <xsl:apply-templates />
   </b>
</xsl:template>
<html>
        <body>
                
        My Cat
        B2
        
                <b>Your Dog</b>
                
        
                
                <b>My Dog</b>
                
        
   
        </body>
</html>
   

Built-in Rules

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="processing-instruction()|comment()"/>

Built-in rules only apply if there is no other match for an element

Just the Dogs!

Input

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   </body></html>
</xsl:template>
   
 <xsl:template match="d">
   <b>
      <xsl:value-of select="." />
   </b>
</xsl:template>
   
<xsl:template match="text()" />
<html>
        <body>
                <b>Your Dog</b>
                <b>My Dog</b>
        </body>
</html>

What Went wrong here?

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   </body></html>
</xsl:template>
   
 <xsl:template match="d">
   <b>
      <xsl:apply-templates />
   </b>
</xsl:template>
   
<xsl:template match="text()" />
<html>
        <body>
                <b></b>
                <b></b>
        </body>
</html>

More Built-in Rules

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

 <xsl:template match="d">
   <b>
      <xsl:value-of select="." />
   </b>
</xsl:template>
   
<xsl:template match="text()" />
<b>Your Dog</b>
<b>My Dog</b>

How is this rule applied to the d tags, which are nested inside the document?

Attributes

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   </body></html>
</xsl:template>
   
<xsl:template match="@*">
   <p>
      <xsl:value-of select="." />
   </p>
</xsl:template>
   
<xsl:template match="text()" />
<html>
        <body>
                <p>sam</p>
                <p>pete</p>
        </body>
</html>
   

Both Attribute & Text Node

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   </body></html>
</xsl:template>
   
<xsl:template match="b">
   <p>
      <xsl:value-of select="@cat" /> =
      <xsl:value-of select="." />
   </p>
</xsl:template>
   
<xsl:template match="text()" />
<html>
        <body>
                <p>sam =
                My Cat</p>
                <p> =
                B2</p>
        </body>
</html>
   

<xsl:if>

<a>
   <b cat="sam">My Cat</b>
   <b>B2</b>
   <d>Your Dog</d>
   <c>
      <d dog="pete">My Dog</d>
   </c>
</a>

XSLT

Result

<xsl:template match="/">
   <html><body>
      <xsl:apply-templates />
   </body></html>
</xsl:template>
   
<xsl:template match="b">
   <xsl:if test="@cat">
   <p>
      <xsl:value-of select="@cat" /> =
      <xsl:value-of select="." />
   </p>
   </xsl:if>
</xsl:template>
   
<xsl:template match="text()" />
<html>
        <body>
                <p>sam =
                My Cat</p>
        </body>
</html>
   

CD Catalog Example

Example from w3schools xsl on-line tutorial

http://www.w3schools.com/xsl/cdcatalog.xml

File catalog.xml

<?xml version="1.0" ?> 
<catalog>
   <cd>
      <title>Empire Burlesque</title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
   </cd>
   <cd>
      <title>Hide your heart</title>
      <artist>Bonnie Tyler</artist>
      <country>UK</country>
      <company>CBS Records</company>
      <price>9.90</price>
      <year>1988</year>
   </cd>
   <cd>
      <title>Greatest Hits</title>
      <artist>Dolly Parton</artist>
      <country>USA</country>
      <company>RCA</company>
      <price>9.90</price>
      <year>1982</year>
   </cd>
</catalog>
   

Does not print all CDs

<xsl:template match="/">
  <html>
  <body>    
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th><th>Artist</th>
      </tr>
      <tr>
      <td><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>

Example from w3schools xsl on-line tutorial

<xsl:for-each>


<?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="/">
  <html>
  <body>    
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th><th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Example from w3schools xsl on-line tutorial

Result

<html>
        <body>
                <table border="1">
                        <tr bgcolor="#9acd32">
                                <th>Title</th>
                                <th>Artist</th>
                        </tr>
                        <tr>
                                <td>Empire Burlesque</td>
                                <td>Bob Dylan</td>
                        </tr>
                        <tr>
                                <td>Hide your heart</td>
                                <td>Bonnie Tyler</td>
                        </tr>
                        <tr>
                                <td>Greatest Hits</td>
                                <td>Dolly Parton</td>
                        </tr>
                </table>
        </body>
</html>

<xsl:sort>

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

Example from w3schools xsl on-line tutorial

<xsl:if>

<xsl:template match="/">
  <html>
  <body>
<table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

Example from w3schools xsl on-line tutorial

<xsl:choose>

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <xsl:choose>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

Example from w3schools xsl on-line tutorial

<xsl:number>

<xsl:template match="/">
  <html>
  <body>
  <table border="1">
      <tr bgcolor="#9acd32">
        <th>Number</th>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
        <xsl:sort select="artist"/>
       <tr>
        <td>  <xsl:number value="position()"/>
        </td>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
       </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
Previous     visitors since 16-Sep-04     Next