XSL
eXtensible Stylesheet Language

Sample XML file
<?xml version="1.0"?>
<aexchange>
 <album id="a29183">
  <title>Electric Ladyland</title>
  <artist>Jimi Hendrix</artist>
  <producer>Jimi Hendrix</producer>
  <label>MCA Records, Inc.</label>
  <year>1968</year>
  <format type="CD" price="14.44" />
  <format type="Cassette" price="8.88" />
 </album>
 <album id="a11105015">
  <title>Love Supreme</title>
  <artist>John Coltrane</artist>
  <label>MCA Records, Inc.</label>
  <manufacturer>
   <name>GRP Records</name>
   <address>
    <street>555 West 57th Street</street>
    <city>New York</city>
    <state>NY</state>
    <zip>10019</zip>
   </address>
  </manufacturer>
  <year>1964</year>
  <format type="LP" price="10.78" />
  <format type="Cassette" price="8.88" />
 </album>
</aexchange>

 

Corresponding DTD: album.dtd
<!-- root element-->
<!-- the store can contain multiple albums -->
<!ELEMENT aexchange (store, album+)>
<!-- store the record came from -->
<!ELEMENT store EMPTY>
<!-- id and the store's name -->
<!ATTLIST store id ID #REQUIRED
                name CDATA #IMPLIED>
<!-- album information -->
<!ELEMENT album (title, artist, producer*, label, manufacturer+, year, format+)>
<!-- unique identifier for album -->
<!ATTLIST album id ID #REQUIRED>
<!-- title of album -->
<!ELEMENT title (#PCDATA)>
<!-- performer of songs -->
<!ELEMENT artist (#PCDATA)>
<!-- producer of album -->
<!ELEMENT producer (#PCDATA)>
<!-- record label -->
<!ELEMENT label (#PCDATA)>
<!-- manufacturer of album formats -->
<!ELEMENT manufacturer (name, address?)>
<!-- name of entity -->
<!ELEMENT name (#PCDATA)>
<!-- address of entity -->
<!ELEMENT address (street*, city?, (state | province)?, zip?, country)>
<!-- street address containing number, street name, PO Box, etc. -->
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!-- for UK addresses -->
<!ELEMENT province (#PCDATA)>
<!-- zip code -->
<!ELEMENT zip (#PCDATA)>
<!-- country is mandatory -->
<!ELEMENT country (#PCDATA)>
<!-- year the album was initally released -->
<!ELEMENT year (#PCDATA)>
<!-- format of the album -->
<!ELEMENT format EMPTY>
<!-- physical format of the album -->
<!-- i.e. LP, cassette, CD, etc. -->
<!ATTLIST format type CDATA #REQUIRED
                 price CDATA #IMPLIED>

 

Sample XML file with Document Type Declaration from album.com
<?xml version="1.0"?>
<!DOCTYPE repository PUBLIC "http://music.com/album.dtd">
<aexchange>
 <store id="s393" name="album.com" />

 <!-- Armon's album -->
 <album id="a573e291">
  <title>Thriller</title>
  <artist>Michael Jackson</artist>
  <producer>Quincy Jones</producer>
  <label>CBS Inc.</label>
  <manufacturer>
   <name>Epic Records</name>
  </manufacturer>
  <manufacturer>
   <name>CBS Inc.</name>
   <address>
    <street>1801 Century Park West</street>
    <city>Los Angeles</city>
    <state>CA</state>
    <country>US</country>
   </address>
  </manufacturer>
  <year>1982</year>
  <format type="CD" price="14.44" />
  <format type="LP" price="12.58" />
 </album>

 <!-- John's favorite album -->
 <album id="a7730103">
  <title>Hotel California</title>
  <artist>Eagles</artist>
  <label>Electra</label>
  <manufacturer>
   <name>Asylum Records</name>
   <address>
    <street>962 North La Contega Boulevard</street>
    <city>Los Angeles</city>
    <state>CA</state>
    <zip>90069</zip>
    <country>US</country>
   </address>
  </manufacturer>
  <year>1964</year>
  <format type="LP" price="11.98" />
 </album>
</aexchange>

 

Basic outline
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
   </body>
  </html>
 </xsl:template>

</xsl:stylesheet>

 

simple_album1.xsl using apply-templates
<?xml version="1.0"?>
<!-- derived from listing 17-5 from XML Bible -->
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <!-- match the root: aexchange --> 
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="album">
   <xsl:apply-templates select="title"/>
 </xsl:template>

</xsl:stylesheet>

 

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

 <!-- match the root: aexchange --> 
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

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

</xsl:stylesheet>

 

Wildcard
<xsl:template match="manuf*">
 <xsl:value-of select="name" />
</xsl:template>

 

With / and //
<xsl:template match="manufacturer/address">
 <xsl:value-of select="country" />
</xsl:template>

<xsl:template match="manufacturer//country">
 <xsl:value-of select="." />
</xsl:template>

 

simple_album3.xsl using for-each
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- match the root: aexchange -->
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="album">
  <table border="0" cellpadding="0" cellspacing="0" width="300">
   <tr>
    <td colspan="2" >
     <b><xsl:value-of select="title" /></b>
    </td>
   </tr>
   <xsl:for-each select="manufacturer">
    <tr>
     <td width="125">Manufacturer</td>
      <td><xsl:value-of select="name" /></td>
     </tr>
    </xsl:for-each>
   </table>
   <p />
  </xsl:template>
</xsl:stylesheet>
 

 

Matching comments
<xsl:template match="comment()">
 <xsl:value-of select="." />
</xsl:template>

 

simple_album4.xsl using for-each and pattern matching
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- match the root: aexchange -->
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="album">
  <table border="0" cellpadding="0" cellspacing="0" width="300">
   <tr>
    <td colspan="2" >
     <b><xsl:value-of select="title" /></b>
    </td>
   </tr>
   <xsl:for-each select="manufacturer/name">
    <tr>
     <td width="125">Manufacturer</td>
      <td><xsl:value-of select="." /></td>
     </tr>
    </xsl:for-each>
   </table>
   <p />
  </xsl:template>
</xsl:stylesheet>
 

 

simple_album5.xsl using for-each and attributes
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- match the root: aexchange -->
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="album">
  <table border="0" cellpadding="0" cellspacing="0" width="300">
   <tr>
    <td colspan="3" >
     <b><xsl:value-of select="title" /></b>
    </td>
   </tr>
   <tr>
    <td>Artist</td>
    <td colspan="2"><xsl:value-of select="artist" /></td>
   </tr>
   <xsl:for-each select="format">
    <tr>
     <td width="125">Format</td>
     <td><xsl:value-of select="@type" /></td>
     <td><xsl:value-of select="@price" /></td>
     </tr>
    </xsl:for-each>
   </table>
   <p />
  </xsl:template>
</xsl:stylesheet>
 

 

simple_album6.xsl using sort
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- match the root: aexchange -->
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="album">
  <table border="0" cellpadding="0" cellspacing="0" width="300">
   <tr>
    <td colspan="3" >
     <b><xsl:value-of select="title" /></b>
    </td>
   </tr>
   <tr>
    <td>Artist</td>
    <td colspan="2"><xsl:value-of select="artist" /></td>
   </tr>
   <xsl:for-each select="format">
    <xsl:sort select="@type" />
    <tr>
     <td width="125">Format</td>
     <td><xsl:value-of select="@type" /></td>
     <td><xsl:value-of select="@price" /></td>
     </tr>
    </xsl:for-each>
   </table>
   <p />
  </xsl:template>
</xsl:stylesheet>
 

 

simple_album6.xsl using sort
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- match the root: aexchange -->
 <xsl:template match="/">
  <html>
   <head>
    <title>New Releases</title>
   </head>
   <body>
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>

 <xsl:template match="album">
  <table border="0" cellpadding="0" cellspacing="0" width="300">
   <tr>
    <td colspan="3" >
     <b><xsl:value-of select="title" /></b>
    </td>
   </tr>
   <tr>
    <td>Artist</td>
    <td colspan="2"><xsl:value-of select="artist" /></td>
   </tr>
   <xsl:for-each select="format">
    <xsl:if test="@price&gt;13">
     <tr>
      <td width="125">Format</td>
      <td><xsl:value-of select="@type" /></td>
      <td><xsl:value-of select="@price" /></td>
     </tr>
    </xsl:if>
   </xsl:for-each>
   </table>
   <p />
  </xsl:template>
</xsl:stylesheet>
 

 

album.com's Stylesheet for display on their site
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!-- match the root -->
 <xsl:template match="/">
<html>
 <head>
  <title>album.com's New Music Releases</title>
 </head>
 <body>
  <xsl:apply-templates />
 </body>
</html>
 </xsl:template>

 <!-- Printing out the store information is unnecessary to 
   users looking for the new releases...they aren't looking 
   for the "behind the scenes" information included in the 
   XML document.
 -->

 <xsl:template match="album">

  <table width="300" cellpadding="0" cellspacing="1" border="0">
   <tr>
    <td colspan="2" bgcolor="#cccccc">
     <b>
      <a href="individual.php?id={@id}">
       <xsl:value-of select="title" />
      </a>
     </b> 
    </td>
   </tr>
   <tr>
    <td bgcolor="#cccc99" width="100">Year: </td>
    <td><xsl:value-of select="year" /></td>
   </tr>
   <tr>
    <td bgcolor="#cccc99">Artist: </td>
    <td><xsl:value-of select="artist" /></td>
   </tr>
   <xsl:for-each select="producer">
    <tr>
     <td bgcolor="#cccc99">Producer: </td>
     <td><xsl:value-of select="." /></td>
    </tr>
   </xsl:for-each>
   <tr>
    <td bgcolor="#cccc99">Record Label: </td>
    <td><xsl:value-of select="label" /></td>
   </tr>
   <xsl:for-each select="manufacturer">
    <tr>
     <td bgcolor="#cccc99">Manufacturer: </td>
     <td><xsl:value-of select="name" /></td>
    </tr>
   </xsl:for-each>
   <xsl:for-each select="format">
    <tr>
     <td bgcolor="#cccc99">Format: </td>
     <td>
      <xsl:value-of select="@type" /> $<xsl:value-of select="@price" />
     </td>
    </tr>
   </xsl:for-each>
  </table>

  <p />

 </xsl:template>
</xsl:stylesheet>