<?xml version="1.0"?>

<!-- Demonstrate some of XSLTs capabilities with the Friends epsiode list
     parameters:
          "atleast"    cut-off for the list of most appearing actors -->


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

  
<xsl:output encoding="ISO-8859-1" method="html" />

  
<xsl:param name="atleast">5</xsl:param>


  
<!-- generate the main framework for the page -->
  
<xsl:template match="/">
    
<html>
      
<head>
        
<title>Friends of XSLTs tricks</title>
        
<link href="series.css" rel="stylesheet" />
      
</head>
      
<body>
        
<h1 align="center">Friends of XSLTs tricks</h1>

        
<hr />

        
<xsl:apply-templates select="series/episodes" />

        
<hr />

        
<xsl:apply-templates select="series/recurring" />

      
</body>
    
</html>
  
</xsl:template>


  
<!-- generate a TOW list -->
  
<xsl:template match="episodes">
    
<xsl:variable name="with">
      
<xsl:text>The One With </xsl:text>
    
</xsl:variable>
    
<xsl:variable name="where">
      
<xsl:text>The One Where </xsl:text>
    
</xsl:variable>
    
<xsl:variable name="one">
      
<xsl:text>The One </xsl:text>
    
</xsl:variable>

    
<table>
      
<tr>
        
<td valign="top"><xsl:value-of select="$with" /></td>
        
<td>
          
<xsl:for-each select="season/episode[starts-with(title, $with)]">
            
<xsl:sort select="title" />
            
<xsl:call-template name="linked_title">
              
<xsl:with-param name="episode" select="." />
              
<xsl:with-param name="title">
                
<xsl:value-of select="substring-after(title, $with)" />
              
</xsl:with-param>
            
</xsl:call-template>
            
<xsl:if test="position()!=last()"></xsl:if>
          
</xsl:for-each>
        
</td>
      
</tr>
      
<tr>
        
<td valign="top"><xsl:value-of select="$where" /></td>
        
<td>
          
<xsl:for-each select="season/episode[starts-with(title, $where)]">
            
<xsl:sort select="title" />
            
<xsl:call-template name="linked_title">
              
<xsl:with-param name="episode" select="." />
              
<xsl:with-param name="title">
                
<xsl:value-of select="substring-after(title, $where)" />
              
</xsl:with-param>
            
</xsl:call-template>
            
<xsl:if test="position()!=last()"></xsl:if>
          
</xsl:for-each>
        
</td>
      
</tr>
      
<tr>
        
<td valign="top"><xsl:value-of select="$one" /></td>
        
<td>
          
<xsl:for-each select="season/episode[not(starts-with(title, $with) or starts-with(title, $where))]">
            
<xsl:sort select="title" />
            
<xsl:call-template name="linked_title">
              
<xsl:with-param name="episode" select="." />
              
<xsl:with-param name="title">
                
<xsl:value-of select="substring-after(title, $one)" />
              
</xsl:with-param>
            
</xsl:call-template>
            
<xsl:if test="position()!=last()"></xsl:if>
          
</xsl:for-each>
        
</td>
      
</tr>
    
</table>
  
</xsl:template>

  
<xsl:template name="linked_title">
    
<xsl:param name="episode" />
    
<xsl:param name="title" />
    
<a href="season{$episode/../@id}.html#{$episode/@id}">
      
<xsl:value-of select="$title" />
    
</a>
  
</xsl:template>



  
<!-- show how often a guest actor has appeared in a recurring role -->
  
<!-- this template processes the data in two steps, first generating
       a result tree fragment, which is subsequently manipulated. we use
       an EXSLT function to convert the tree to a node-set. -->

  
<xsl:template match="recurring">
    
<xsl:variable name="countlist">
      
<xsl:call-template name="count-recurring" />
    
</xsl:variable>
    
<p>
      Actors with at least 
<xsl:value-of select="$atleast" /> appearances:
    
</p>
    
<table width="100%">
      
<xsl:for-each select="exslt:node-set($countlist)/member">
        
<xsl:sort data-type="number" order="descending" select="count" />
        
<xsl:if test="count >= $atleast">
          
<tr>
            
<td width="30%">
              
<a href="actors.html#{id}"><xsl:value-of select="actor" /></a>
            
</td>
            
<td>appears in</td>
            
<td align="right">
              
<xsl:value-of select="count" /> epsiodes
            
</td>
            
<td>
              as
            
</td>
            
<td width="30%">
              
<xsl:value-of select="role" />
            
</td>
          
</tr>
        
</xsl:if>
      
</xsl:for-each>
    
</table>
  
</xsl:template>

  
<xsl:template name="count-recurring">
    
<xsl:for-each select="/series/recurring/member">
      
<xsl:variable name="id"><xsl:value-of select="@id" /></xsl:variable>
      
<member>
        
<id><xsl:value-of select="$id" /></id>
        
<xsl:copy-of select="actor" />
        
<xsl:copy-of select="role" />
        
<count><xsl:value-of select="count(//cast/member[@ref=$id])" /></count>
      
</member>
    
</xsl:for-each>
  
</xsl:template>


</xsl:stylesheet>