XSL Transformations


XSL Transformations

Elliotte Rusty Harold

Software Development 2001 West April 10, 2001

elharo@metalab.unc.edu

http://www.ibiblio.org/xml/


What Is XSL?


Versions


The Process of an XSL Transformation

  1. The XML parser reads an XML document and forms a tree

  2. The tree is passed to the XSLT processor

  3. The XSLT processor compares the nodes in the tree to the instructions in the style sheet

  4. When the XSLT processor finds a match, it outputs a tree fragment

  5. (Optional) The complete output tree is serialized to some other format such as text, HTML, or an XML file


An Example XML Document

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>

  <category>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </category>

  <cataloging_info>
    <abstract>Compositions by the members of New York Women Composers</abstract>
    <keyword>music publishing</keyword>
    <keyword>scores</keyword>
    <keyword>women composers</keyword>
    <keyword>New York</keyword>
  </cataloging_info>

  <last_updated>July 28, 1999</last_updated>
  <copyright>1999 New York Women Composers</copyright>
  <maintainer email="elharo@metalab.unc.edu" 
              url="http://www.macfaq.com/personal.html">
    <name>
      <first_name>Elliotte</first_name> 
      <middle_name>Rusty</middle_name> 
      <last_name>Harold</last_name>
    </name>
  </maintainer>

  <composer id="c1">
    <name>
      <first_name>Julie</first_name> 
      <middle_name></middle_name> 
      <last_name>Mandel</last_name>
    </name>
  </composer>

  <composer id="c2">
    <name>
      <first_name>Margaret</first_name> 
      <middle_name>De</middle_name> 
      <last_name>Wys</last_name>
    </name>
  </composer>  
    
  <composer id="c3">
    <name>
      <first_name>Beth</first_name> 
      <middle_name></middle_name> 
      <last_name>Anderson</last_name>
    </name>
  </composer>
    
  <composer id="c4">
    <name>
      <first_name>Linda</first_name> 
      <middle_name></middle_name> 
      <last_name>Bouchard</last_name>
    </name>
  </composer>
    
  <composition composer="c1">
    <title>Trio for Flute, Viola and Harp</title>
    <date><year>(1994)</year></date> 
    <length>13'38"</length>
    <instruments>fl, hp, vla</instruments>
    <description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
    <publisher>Theodore Presser</publisher>
  </composition>

  <composition composer="c2">
    <title>Charmonium</title>
    <date><year>(1991)</year></date> 
    <length>9'</length>
    <instruments>2 vln, vla, vc</instruments>
    <description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description> 
    <publisher></publisher>
  </composition>

  <composition composer="c1">
    <title>Invention for Flute and Piano</title>
    <date><year>(1994)</year></date> 
    <length></length>
    <instruments>fl, pn</instruments>
    <description>3 movements</description> 
    <publisher></publisher>
  </composition>

  <composition composer="c3">
    <title>Little Trio</title>
    <date><year>(1984)</year></date> 
    <length>4'</length>
    <instruments>fl, guit, va</instruments>
    <description></description> 
    <publisher>ACA</publisher>
  </composition>

  <composition composer="c3">
    <title>Dr. Blood's Mermaid Lullaby</title>
    <date><year>(1980)</year></date> 
    <length>3'</length>
    <instruments>fl or ob, or vn, or vc, pn</instruments>
    <description></description> 
    <publisher>ACA</publisher>
  </composition>

  <composition composer="c3">
    <title>Trio: Dream in D</title>
    <date><year>(1980)</year></date> 
    <length>10'</length>
    <instruments>fl, pn, vc, or vn, pn, vc</instruments>
    <description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description> 
    <publisher></publisher>
  </composition>

  <composition composer="c4">
    <title>Propos II</title>
    <date><year>(1985)</year></date> 
    <length>11'</length>
    <instruments>2 tpt</instruments>
    <description>Arrangement from Propos</description> 
    <publisher></publisher>
  </composition>

  <composition composer="c4">
    <title>Rictus En Mirroir</title>
    <date><year>(1985)</year></date> 
    <length>14'</length>
    <instruments>fl, ob, hpschd, vc</instruments>
    <description></description> 
    <publisher></publisher>
  </composition>

</catalog>

An XSLT Style Sheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



</xsl:stylesheet>

Let's use xt to apply this stylesheet to compositions.xml.
















Windows executable:

C:> xt compositions.xml sheet1.xsl

Java executable:

C:> java -Dcom.jclark.xsl.sax.parser=com.jclark.xml.sax.CommentDriver
com.jclark.xsl.sax.Driver compositions.xml sheet1.xsl output1.html 

Output

<?xml version="1.0" encoding="utf-8"?>


  
    Small chamber ensembles - 2-4 Players by New York Women Composers
  

  
    Compositions by the members of New York Women Composers
    music publishing
    scores
    women composers
    New York
  

  July 28, 1999
  1999 New York Women Composers
  
    
      Elliotte 
      Rusty 
      Harold
    
  

  
    
      Julie 
       
      Mandel
    
  

  
    
      Margaret 
      De 
      Wys
    
    
    
  
    
      Beth 
       
      Anderson
    
  
    
  
    
      Linda 
       
      Bouchard
    
  
    
  
    Trio for Flute, Viola and Harp
    (1994) 
    13'38"
    fl, hp, vla
    
      Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :
      
        mvt. 1: 5:01
        mvt. 2: 4:11
        mvt. 3: 4:26
        
    
    Theodore Presser
  

  
    Charmonium
    (1991) 
    9'
    2 vln, vla, vc
    
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
     
    
  

  
    Invention for Flute and Piano
    (1994) 
    
    fl, pn
    3 movements 
    
  

  
    Little Trio
    (1984) 
    4'
    fl, guit, va
     
    ACA
  

  
    Dr. Blood's Mermaid Lullaby
    (1980) 
    3'
    fl or ob, or vn, or vc, pn
     
    ACA
  

  
    Trio: Dream in D
    (1980) 
    10'
    fl, pn, vc, or vn, pn, vc
    
      Rhapsodic. Passionate. Available on CD 
      Two by Three 
      from North/South Consonance (1998).
     
    
  

  
    Propos II
    (1985) 
    11'
    2 tpt
    Arrangement from Propos 
    
  

  
    Rictus En Mirroir
    (1985) 
    14'
    fl, ob, hpschd, vc
     
    
  

View in Netscape

Templates

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="composition">
    <h3><xsl:value-of select="title"/></h3>
  </xsl:template>

</xsl:stylesheet>
View Transformed Document in Netscape

The actual HTML output

<?xml version="1.0" encoding="utf-8"?>


  
    Small chamber ensembles - 2-4 Players by New York Women Composers
  

  
    Compositions by the members of New York Women Composers
    music publishing
    scores
    women composers
    New York
  

  July 28, 1999
  1999 New York Women Composers
  
    
      Elliotte 
      Rusty 
      Harold
    
  

  
    
      Julie 
       
      Mandel
    
  

  
    
      Margaret 
      De 
      Wys
    
    
    
  
    
      Beth 
       
      Anderson
    
  
    
  
    
      Linda 
       
      Bouchard
    
  
    
  <h3>Trio for Flute, Viola and Harp</h3>

  <h3>Charmonium</h3>

  <h3>Invention for Flute and Piano</h3>

  <h3>Little Trio</h3>

  <h3>Dr. Blood's Mermaid Lullaby</h3>

  <h3>Trio: Dream in D</h3>

  <h3>Propos II</h3>

  <h3>Rictus En Mirroir</h3>


Adding the root

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="composition">
    <h3><xsl:value-of select="title"/></h3>
  </xsl:template>

</xsl:stylesheet>
View Transformed Document in Netscape

The actual HTML output

<html>
<body>
</body>
</html>

Applying Templates

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="composition">
    <h3><xsl:value-of select="title"/></h3>
  </xsl:template>

</xsl:stylesheet>

View Transformed Document in Netscape

The actual HTML output

<html>
<body>

  
    Small chamber ensembles - 2-4 Players by New York Women Composers
  

  
    Compositions by the members of New York Women Composers
    music publishing
    scores
    women composers
    New York
  

  July 28, 1999
  1999 New York Women Composers
  
    
      Elliotte 
      Rusty 
      Harold
    
  

  
    
      Julie 
       
      Mandel
    
  

  
    
      Margaret 
      De 
      Wys
    
    
    
  
    
      Beth 
       
      Anderson
    
  
    
  
    
      Linda 
       
      Bouchard
    
  
    
  <h3>Trio for Flute, Viola and Harp</h3>

  <h3>Charmonium</h3>

  <h3>Invention for Flute and Piano</h3>

  <h3>Little Trio</h3>

  <h3>Dr. Blood's Mermaid Lullaby</h3>

  <h3>Trio: Dream in D</h3>

  <h3>Propos II</h3>

  <h3>Rictus En Mirroir</h3>

</body>
</html>

The select attribute

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
       <xsl:apply-templates select="catalog"/>
    </html>
  </xsl:template>

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <h1><xsl:value-of select="category"/></h1>      
       <xsl:apply-templates select="composition"/>
    </body>
  </xsl:template>

  <xsl:template match="composition">
    <h3><xsl:value-of select="title"/></h3>
  </xsl:template>

</xsl:stylesheet>
View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h3>Trio for Flute, Viola and Harp</h3>
<h3>Charmonium</h3>
<h3>Invention for Flute and Piano</h3>
<h3>Little Trio</h3>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<h3>Trio: Dream in D</h3>
<h3>Propos II</h3>
<h3>Rictus En Mirroir</h3>
</body>
</html>

Expanding the coverage

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
       <xsl:apply-templates select="catalog"/>
    </html>
  </xsl:template>

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <h1><xsl:value-of select="category"/></h1>      
       <xsl:apply-templates select="composition"/>
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/>
    </body>
  </xsl:template>

  <xsl:template match="composition">
    <h3><xsl:value-of select="title"/></h3>

    <ul>
     <li><xsl:value-of select="date"/></li>
     <li><xsl:value-of select="length"/></li>
     <li><xsl:value-of select="instruments"/></li>
     <li><xsl:value-of select="publisher"/></li>    
    </ul>

    <p><xsl:value-of select="description"/></p>    
    
  </xsl:template>

</xsl:stylesheet>
View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
      Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :
      
        mvt. 1: 5:01
        mvt. 2: 4:11
        mvt. 3: 4:26
        
    </p>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
<li>
</li>
</ul>
<p>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>
</li>
<li>fl, pn</li>
<li>
</li>
</ul>
<p>3 movements</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
<li>
</li>
</ul>
<p>
      Rhapsodic. Passionate. Available on CD 
      Two by Three 
      from North/South Consonance (1998).
    </p>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
<li>
</li>
</ul>
<p>Arrangement from Propos</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
<li>
</li>
</ul>
<p>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999</body>
</html>

Attributes


View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
      Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :
      
        mvt. 1: 5:01
        mvt. 2: 4:11
        mvt. 3: 4:26
        
    </p>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
<li>
</li>
</ul>
<p>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>
</li>
<li>fl, pn</li>
<li>
</li>
</ul>
<p>3 movements</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
<li>
</li>
</ul>
<p>
      Rhapsodic. Passionate. Available on CD 
      Two by Three 
      from North/South Consonance (1998).
    </p>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
<li>
</li>
</ul>
<p>Arrangement from Propos</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
<li>
</li>
</ul>
<p>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
      Elliotte 
      Rusty 
      Harold
    <br>elharo@metalab.unc.edu</body>
</html>

Attribute Value Templates


View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
      Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :
      
        mvt. 1: 5:01
        mvt. 2: 4:11
        mvt. 3: 4:26
        
    </p>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
<li>
</li>
</ul>
<p>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>
</li>
<li>fl, pn</li>
<li>
</li>
</ul>
<p>3 movements</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
<li>
</li>
</ul>
<p>
      Rhapsodic. Passionate. Available on CD 
      Two by Three 
      from North/South Consonance (1998).
    </p>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
<li>
</li>
</ul>
<p>Arrangement from Propos</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
<li>
</li>
</ul>
<p>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

xsl:copy


View Transformed Document in Netscape

The generated HTML

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </p>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
<li>
</li>
</ul>
<p>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>
</li>
<li>fl, pn</li>
<li>
</li>
</ul>
<p>3 movements</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
<li>
</li>
</ul>
<p>
      Rhapsodic. Passionate. Available on CD 
      <cite>Two by Three</cite> 
      from North/South Consonance (1998).
    </p>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
<li>
</li>
</ul>
<p>Arrangement from Propos</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
<li>
</li>
</ul>
<p>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

The Or Operator in Match Patterns

Since all four template rules for the HTML element have the same content, we can combine them into a single rule that applies to each of the four using the or operator |

  <xsl:template match="p|ul|li|cite">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>  

The output is the same

The * wildcard

Right now the descriptions in the input document only use a few HTML tags, but potentially they could use full HTML up to and including tables, images, styles, and more. You could include separate template rules for each of these, but it's easier to specify a rule that applies to all elements.

  <!-- pass unrecognized tags along unchanged -->
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>  

The * matches all elements that are not matched by some more specific rules. It only matches element nodes, though. It does not match nodes for


The output is the same in this case, though for a document that used more HTML it might be different.


The node() and @* wildcards

To copy everything including:

we have to use greedier wild cards:

  <!-- pass unrecognized nodes along unchanged -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>  

xt doesn't yet recognize node() in match patterns


The output is the same in this case, though for a document that used more HTML it might be different.


Are we being too greedy?

Perhaps this is too greedy. Do we really only want to recognize HTML in the description element? What if somebody puts HTML in a different, element like instruments? What if somebody makes a mistake and adds an element that shouldn't be there?

I don't think so, but it would be possible to use modes or other techniques to make this default rule only apply inside the description element.


Testing conditions with xsl:if

  <xsl:template match="composition">
    <h3><xsl:value-of select="title"/></h3>

    <ul>
     <xsl:if test="string(date)">
       <li><xsl:value-of select="date"/></li>
     </xsl:if>
     <xsl:if test="string(length)">
       <li><xsl:value-of select="length"/></li>
     </xsl:if>
     <xsl:if test="string(instruments)">
       <li><xsl:value-of select="instruments"/></li>
     </xsl:if>
     <xsl:if test="string(publisher)">
       <li><xsl:value-of select="publisher"/></li>
     </xsl:if>    
    </ul>

    <p><xsl:apply-templates select="description"/></p>    
    
  </xsl:template>

View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Matching composers with compositions

The composers and their compositions are linked through the the id attribute of the composer element and the composer attribute of the composition element.

  <composer id="c3">
    <name>
      <first_name>Beth</first_name> 
      <middle_name></middle_name> 
      <last_name>Anderson</last_name>
    </name>
  </composer>
    
  <composition composer="c3">
    <title>Trio: Dream in D</title>
    <date><year>(1980)</year></date> 
    <length>10'</length>
    <instruments>fl, pn, vc, or vn, pn, vc</instruments>
    <description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description> 
    <publisher></publisher>
  </composition>

Matching composers with compositions

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <h1><xsl:value-of select="category"/></h1>      
       <xsl:apply-templates select="composer"/>
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template>
  
  <xsl:template match="composer">
    <h2><xsl:value-of select="name"/></h2>
    <xsl:apply-templates select="../composition[@composer=current()/@id]"/>
  </xsl:template>

View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Sorting with xsl:sort

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <h1><xsl:value-of select="category"/></h1>      
       <xsl:apply-templates select="composer">
         <xsl:sort select="name/last_name"/>
       </xsl:apply-templates>
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template>


View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Multiple Key Sorts

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <h1><xsl:value-of select="category"/></h1>      
       <xsl:apply-templates select="composer">
         <xsl:sort select="name/last_name"/>
         <xsl:sort select="name/first_name"/>
         <xsl:sort select="name/middle_name"/>
       </xsl:apply-templates>
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template>

View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Sorting by Composition Title

Sorting by composition title is equally straight-forward but we have to do it in a separate apply-templates element

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <h1><xsl:value-of select="category"/></h1>      
       <xsl:apply-templates select="composer">
         <xsl:sort select="name/last_name"/>
         <xsl:sort select="name/first_name"/>
         <xsl:sort select="name/middle_name"/>
       </xsl:apply-templates>
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template>
  
  <xsl:template match="composer">
    <h2><xsl:value-of select="name"/></h2>
    <xsl:apply-templates select="../composition[@composer=current()/@id]">
       <xsl:sort select="title"/>      
    </xsl:apply-templates>
  </xsl:template>

View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

xsl:for-each

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <!-- Header -->            
       <h1><xsl:value-of select="category"/></h1>
       <ul>
         <xsl:for-each select="composition">
           <li><xsl:value-of select="title"/></li>
         </xsl:for-each>
       </ul>
       
       <!-- Body -->            
       <xsl:apply-templates select="composer">
         <xsl:sort select="name/last_name"/>
         <xsl:sort select="name/first_name"/>
         <xsl:sort select="name/middle_name"/>
       </xsl:apply-templates>
       
       <!-- Signature -->      
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template>

View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<ul>
<li>Trio for Flute, Viola and Harp</li>
<li>Charmonium</li>
<li>Invention for Flute and Piano</li>
<li>Little Trio</li>
<li>Dr. Blood's Mermaid Lullaby</li>
<li>Trio: Dream in D</li>
<li>Propos II</li>
<li>Rictus En Mirroir</li>
</ul>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>Dr. Blood's Mermaid Lullaby</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Little Trio</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>Trio: Dream in D</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>Propos II</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>Rictus En Mirroir</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>Invention for Flute and Piano</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h3>Trio for Flute, Viola and Harp</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>Charmonium</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Sorting the Table of Contents

xsl:for-each can have an xsl:sort child just like xsl:apply-templates

  <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <!-- Header -->            
       <h1><xsl:value-of select="category"/></h1>
       <ul>
         <xsl:for-each select="composition">
           <xsl:sort select="title"/>
           <li><xsl:value-of select="title"/></li>
         </xsl:for-each>
       </ul>
       
       <!-- Body -->            
       <xsl:apply-templates select="composer">
         <xsl:sort select="name/last_name"/>
         <xsl:sort select="name/first_name"/>
         <xsl:sort select="name/middle_name"/>
       </xsl:apply-templates>
       
       <!-- Signature -->      
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template>

View Transformed Document in Netscape

Adding Links from the Table of Contents

Here's the new template for the composition

  <xsl:template match="composition">
    <h3>
      <a name="{generate-id()}">
        <xsl:value-of select="title"/>
      </a>
    </h3>

    <ul>
     <xsl:if test="string(date)">
       <li><xsl:value-of select="date"/></li>
     </xsl:if>
     <xsl:if test="string(length)">
       <li><xsl:value-of select="length"/></li>
     </xsl:if>
     <xsl:if test="string(instruments)">
       <li><xsl:value-of select="instruments"/></li>
     </xsl:if>
     <xsl:if test="string(publisher)">
       <li><xsl:value-of select="publisher"/></li>
     </xsl:if>    
    </ul>

    <p><xsl:apply-templates select="description"/></p>    
    
  </xsl:template>

Here's the new template for the tabel of contents link

   <xsl:template match="catalog">
    <head>
       <title><xsl:value-of select="category"/></title>      
    </head>
    <body>
       <!-- Header -->            
       <h1><xsl:value-of select="category"/></h1>
       <ul>
         <xsl:for-each select="composition">
           <xsl:sort select="title"/>
           <li>
             <a href="#{generate-id()}">
               <xsl:value-of select="title"/>
             </a>
           </li>
         </xsl:for-each>
       </ul>
       
       <!-- Body -->            
       <xsl:apply-templates select="composer">
         <xsl:sort select="name/last_name"/>
         <xsl:sort select="name/first_name"/>
         <xsl:sort select="name/middle_name"/>
       </xsl:apply-templates>
       
       <!-- Signature -->      
       <hr/>
       Copyright <xsl:value-of select="copyright"/><br/>
       Last Modified: <xsl:value-of select="last_updated"/><br/>
       <xsl:apply-templates select="maintainer"/>
    </body>
  </xsl:template> 

Although the ID is generated in two separate places, it is generated for the same node. Consequently, they are the same.


View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<ul>
<li>
<a href="#N146">Charmonium</a>
</li>
<li>
<a href="#N211">Dr. Blood's Mermaid Lullaby</a>
</li>
<li>
<a href="#N168">Invention for Flute and Piano</a>
</li>
<li>
<a href="#N189">Little Trio</a>
</li>
<li>
<a href="#N260">Propos II</a>
</li>
<li>
<a href="#N282">Rictus En Mirroir</a>
</li>
<li>
<a href="#N233">Trio: Dream in D</a>
</li>
<li>
<a href="#N108">Trio for Flute, Viola and Harp</a>
</li>
</ul>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>
<a name="N211">Dr. Blood's Mermaid Lullaby</a>
</h3>
<ul>
<li>(1980)</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>
<a name="N189">Little Trio</a>
</h3>
<ul>
<li>(1984)</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>
<a name="N233">Trio: Dream in D</a>
</h3>
<ul>
<li>(1980)</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>
<a name="N260">Propos II</a>
</h3>
<ul>
<li>(1985)</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>
<a name="N282">Rictus En Mirroir</a>
</h3>
<ul>
<li>(1985)</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>
<a name="N168">Invention for Flute and Piano</a>
</h3>
<ul>
<li>(1994)</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h3>
<a name="N108">Trio for Flute, Viola and Harp</a>
</h3>
<ul>
<li>(1994)</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>
<a name="N146">Charmonium</a>
</h3>
<ul>
<li>(1991)</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Numbering Output

  <xsl:template match="composition">
    <h3><xsl:number value="position()"/>.
      <a name="{generate-id()}">
        <xsl:value-of select="title"/>
      </a>
    </h3>

    <ul>
     <xsl:if test="string(date)">
       <li><xsl:value-of select="date"/></li>
     </xsl:if>
     <xsl:if test="string(length)">
       <li><xsl:value-of select="length"/></li>
     </xsl:if>
     <xsl:if test="string(instruments)">
       <li><xsl:value-of select="instruments"/></li>
     </xsl:if>
     <xsl:if test="string(publisher)">
       <li><xsl:value-of select="publisher"/></li>
     </xsl:if>    
    </ul>

    <p><xsl:apply-templates select="description"/></p>    
    
  </xsl:template>

View Transformed Document in Netscape

String functions


substring()

  <xsl:template match="composition">
    <h3><xsl:number value="position()"/>.
      <a name="{generate-id()}">
        <xsl:value-of select="title"/>
      </a>
    </h3>

    <ul>
     <xsl:if test="string(date)">
       <!--not Y10K safe! -->
       <li><xsl:value-of select="substring(date,2,4)"/></li>
     </xsl:if>
     <xsl:if test="string(length)">
       <li><xsl:value-of select="length"/></li>
     </xsl:if>
     <xsl:if test="string(instruments)">
       <li><xsl:value-of select="instruments"/></li>
     </xsl:if>
     <xsl:if test="string(publisher)">
       <li><xsl:value-of select="publisher"/></li>
     </xsl:if>    
    </ul>

    <p><xsl:apply-templates select="description"/></p>    
    
  </xsl:template>

View Transformed Document in Netscape

The actual HTML output

<html>
<head>
<title>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </title>
</head>
<body>
<h1>
    Small chamber ensembles - 2-4 Players by New York Women Composers
  </h1>
<ul>
<li>
<a href="#N146">Charmonium</a>
</li>
<li>
<a href="#N211">Dr. Blood's Mermaid Lullaby</a>
</li>
<li>
<a href="#N168">Invention for Flute and Piano</a>
</li>
<li>
<a href="#N189">Little Trio</a>
</li>
<li>
<a href="#N260">Propos II</a>
</li>
<li>
<a href="#N282">Rictus En Mirroir</a>
</li>
<li>
<a href="#N233">Trio: Dream in D</a>
</li>
<li>
<a href="#N108">Trio for Flute, Viola and Harp</a>
</li>
</ul>
<h2>
      Beth 
       
      Anderson
    </h2>
<h3>1.
      <a name="N211">Dr. Blood's Mermaid Lullaby</a>
</h3>
<ul>
<li>1980</li>
<li>3'</li>
<li>fl or ob, or vn, or vc, pn</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>2.
      <a name="N189">Little Trio</a>
</h3>
<ul>
<li>1984</li>
<li>4'</li>
<li>fl, guit, va</li>
<li>ACA</li>
</ul>
<p>
<description></description>
</p>
<h3>3.
      <a name="N233">Trio: Dream in D</a>
</h3>
<ul>
<li>1980</li>
<li>10'</li>
<li>fl, pn, vc, or vn, pn, vc</li>
</ul>
<p>
<description>
      Rhapsodic. Passionate. Available on CD 
      <cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite> 
      from North/South Consonance (1998).
    </description>
</p>
<h2>
      Linda 
       
      Bouchard
    </h2>
<h3>1.
      <a name="N260">Propos II</a>
</h3>
<ul>
<li>1985</li>
<li>11'</li>
<li>2 tpt</li>
</ul>
<p>
<description>Arrangement from Propos</description>
</p>
<h3>2.
      <a name="N282">Rictus En Mirroir</a>
</h3>
<ul>
<li>1985</li>
<li>14'</li>
<li>fl, ob, hpschd, vc</li>
</ul>
<p>
<description></description>
</p>
<h2>
      Julie 
       
      Mandel
    </h2>
<h3>1.
      <a name="N168">Invention for Flute and Piano</a>
</h3>
<ul>
<li>1994</li>
<li>fl, pn</li>
</ul>
<p>
<description>3 movements</description>
</p>
<h3>2.
      <a name="N108">Trio for Flute, Viola and Harp</a>
</h3>
<ul>
<li>1994</li>
<li>13'38"</li>
<li>fl, hp, vla</li>
<li>Theodore Presser</li>
</ul>
<p>
<description>
      <p>Premiered at Queens College in April, 1996 by Sue Ann Kahn, 
      Christine Ims, and Susan Jolles. In 3 movements :</p>
      <ul>
        <li>mvt. 1: 5:01</li>
        <li>mvt. 2: 4:11</li>
        <li>mvt. 3: 4:26</li>
      </ul>  
    </description>
</p>
<h2>
      Margaret 
      De 
      Wys
    </h2>
<h3>1.
      <a name="N146">Charmonium</a>
</h3>
<ul>
<li>1991</li>
<li>9'</li>
<li>2 vln, vla, vc</li>
</ul>
<p>
<description>
      Commissioned as quartet for the Meridian String Quartet. 
      Sonorous, bold. Moderate difficulty. Tape available.
    </description>
</p>
<hr>
       Copyright 1999 New York Women Composers<br>
       Last Modified: July 28, 1999<br>
<a href="http://www.macfaq.com/personal.html">
      Elliotte 
      Rusty 
      Harold
    </a>
<br>
<a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a>
</body>
</html>

Number Operators


Number Functions

XPath includes five functions that operate on numbers:


Where Does the Transformation Happen?

There are three primary ways XML documents are transformed into other formats, such as HTML, with an XSLT style sheet:


Client Side Processing

<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="compositions.xsl"?>

A style sheet that works with Internet Explorer 5.0 and 5.5

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

  <xsl:template match="/">
    <html>
      <xsl:apply-templates select="catalog"/>
    </html>
  </xsl:template>

  <xsl:template match="catalog">
    <body>
      <xsl:apply-templates select="composition"/>
    </body>
  </xsl:template>

  <xsl:template match="composition">
    <h3><xsl:value-of select="name"/></h3>
  </xsl:template>

</xsl:stylesheet>

What else does XSLT have?


What does XSLT not have?


Summary


To Learn More


Index | Cafe con Leche

Copyright 1999-2001 Elliotte Rusty Harold
Elliotte Rusty Harold
Last Modified February 17, 2001