<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xdoc="http://www.pnp-software.com/XSLTdoc" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mods="http://www.loc.gov/mods/v3" xmlns:exist="http://exist.sourceforge.net/NS/exist" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:course="http://purl.org/NET/xbiblio/course" xmlns:db="http://docbook.org/ns/docbook" xmlns:bib="http://purl.org/NET/xbiblio/citeproc" xmlns:cs="http://purl.org/NET/xbiblio/csl" version="2.0" exclude-result-prefixes="xdoc mods xs bib exist course db cs">
  
<!-- === licensed under CC-GNU GPL; see http://creativecommons.org/licenses/GPL/2.0 === -->
  
  <xdoc:doc type="stylesheet">
    
<xdoc:short>Handles sorting.</xdoc:short>
    
<xdoc:author>Bruce D’Arcus</xdoc:author>
    
<xdoc:copyright>2004, Bruce D’Arcus</xdoc:copyright>
  
</xdoc:doc>

  
<!-- =================================================================== -->
  
<!-- |    begin bibliography templates                                 | -->
  
<!-- =================================================================== -->
  
  <xdoc:doc>Grouping, sorting and parameter setting to properly handle the
    author-year class bibliographies.
</xdoc:doc>

  
<xsl:template match="mods:modsCollection" mode="sort_author-year">
    
<xsl:variable name="bibref" select="mods:mods" />
    
<xsl:for-each-group select="$bibref" group-by="mods:grouping-key(.)">
      
<xsl:sort select="current-grouping-key()" />
      
<xsl:variable name="bibrefs-for-author-sorted-by-year" as="element(mods:mods)*">
        
<xsl:perform-sort select="current-group()">
          
<xsl:sort select="mods:year(.)" />
          
<!-- add third-level sort on month-day -->
        
</xsl:perform-sort>
      
</xsl:variable>
      
<xsl:variable name="first-bibref-for-author" as="element(mods:mods)" select="$bibrefs-for-author-sorted-by-year[1]" />
      
<xsl:variable name="sort-on" select="current-grouping-key()" />
      
<xsl:for-each-group select="$bibrefs-for-author-sorted-by-year" group-adjacent="mods:year(.)">
        
<xsl:for-each select="current-group()">
          
<xsl:variable name="year-suffix">
            
<xsl:if test="last() &gt; 1">
              
<xsl:number value="position()" format="a" />
            
</xsl:if>
          
</xsl:variable>
          
<xsl:variable name="shorten-author" as="xs:boolean" select="$style-biblio/@author-shorten='yes' and not(. is $first-bibref-for-author)" />
          
<xsl:apply-templates select="." mode="enhance">
            
<xsl:with-param name="year" select="current-grouping-key()" />
            
<xsl:with-param name="year-suffix" select="$year-suffix" />
            
<xsl:with-param name="shorten-author" select="$shorten-author" as="xs:boolean" />
            
<xsl:with-param name="sort-on" select="$sort-on" />
          
</xsl:apply-templates>
        
</xsl:for-each>
      
</xsl:for-each-group>
    
</xsl:for-each-group>
  
</xsl:template>
  
<xsl:template match="mods:modsCollection" mode="sort_cited">
    
<xsl:variable name="sort-on" select="$cite-position/bib:refs/bib:unique/bib:ref[@key='{@ID}']/@position" />
    
<xsl:for-each select="mods:mods">
      
<xsl:sort select="$sort-on" />
      
<xsl:apply-templates select="." mode="enhance">
        
<xsl:with-param name="year" select="mods:year(.)" />
        
<xsl:with-param name="sort-on" select="$sort-on" />
      
</xsl:apply-templates>
    
</xsl:for-each>
  
</xsl:template>
  
<xsl:template match="mods:modsCollection" mode="sort_citekey">
    
<xsl:for-each select="mods:mods">
      
<xsl:sort select="@ID" />
      
<xsl:apply-templates select="." mode="enhance">
        
<xsl:with-param name="year" select="mods:year(.)" />
        
<xsl:with-param name="sort-on" select="@ID" />
      
</xsl:apply-templates>
    
</xsl:for-each>
  
</xsl:template>
  
  <xdoc:doc>Adds data necessary for further processing to the in-memory
    bibliographic list, so it need not be calcuated subsequently.
</xdoc:doc>

  
<xsl:template match="mods:mods" mode="enhance">
    
<xsl:param name="shorten-author" />
    
<xsl:param name="year" />
    
<xsl:param name="year-suffix" />
    
<xsl:param name="sort-on" />
    
<mods:mods ID="{@ID}" bib:shorten-author="{$shorten-author}" bib:refclass="{mods:refclass(.)}" bib:reftype="{mods:reftype(.)}" bib:use-reftype="{mods:use_csl-reftype(.)}" bib:sort-on="{$sort-on}" bib:biblist-position="{position()}">
      
<bib:year>
        
<xsl:value-of select="$year" />
      
</bib:year>
      
<xsl:if test="$year-suffix">
        
<bib:year-suffix>
          
<xsl:value-of select="$year-suffix" />
        
</bib:year-suffix>
      
</xsl:if>
      
<xsl:if test="not(mods:name[mods:role/mods:roleTerm=$primary-contributors])">
        
<bib:noname-substitute>
          
<xsl:value-of select="mods:grouping-key(.)" />
        
</bib:noname-substitute>
      
</xsl:if>
      
<xsl:copy-of select="*" />
    
</mods:mods>
  
</xsl:template>
  
<!-- =================================================================== -->
  
<!-- |    begin citation templates                                     | -->
  
<!-- =================================================================== -->
  
  <xdoc:doc>Grouping/sorting author-year class citations.</xdoc:doc>

  
<xsl:template match="db:citation" mode="sort_citation_author-year">
    
<!-- store citation for future use -->
    
<xsl:variable name="citation" select="." as="element()" />
    
<xsl:variable name="local-cite-style" select="db:biblioref/@xrefstyle" as="node()?" />
    
<xsl:variable name="idref" select="db:biblioref/@linkend, course:reading/@refid" />
    
<xsl:variable name="bibref" select="$enhanced-biblist/mods:modsCollection/mods:mods[@ID=$idref]" />
      
<xsl:for-each-group select="$bibref" group-by="@bib:sort-on">
        
<xsl:sort select="current-grouping-key()" />
        
<xsl:variable name="refs-for-author-sorted-by-year" as="element(mods:mods)*">
          
<xsl:perform-sort select="current-group()">
            
<xsl:sort select="bib:year" />
          
</xsl:perform-sort>
        
</xsl:variable>
        
<xsl:variable name="first-ref-for-author" as="element(mods:mods)" select="$refs-for-author-sorted-by-year[1]" />
        
<xsl:variable name="last-ref-for-author" as="element(mods:mods)" select="$refs-for-author-sorted-by-year[position() = last()]" />
        
<xsl:for-each-group select="$refs-for-author-sorted-by-year" group-adjacent="bib:year">
          
<xsl:for-each select="current-group()">
            
<xsl:variable name="first-in-author-group" select=". is $first-ref-for-author" as="xs:boolean" />
            
<xsl:variable name="last-in-author-group" select=". is $last-ref-for-author" as="xs:boolean" />
            
<xsl:variable name="id" select="@ID" />
            
<!-- earlier the cite-ref variable was defined as just element(), but
            for some reason this yielded an empty-sequence error in one case 
-->
            
<xsl:variable name="cite-ref" as="node()">
              
<xsl:sequence select="$citation/db:biblioref[@linkend=$id]" />
            
</xsl:variable>
            
<xsl:apply-templates select="$style-citation">
              
<xsl:with-param name="first-in-author-group" select="$first-in-author-group" as="xs:boolean" tunnel="yes" />
              
<xsl:with-param name="last-in-author-group" select="$last-in-author-group" as="xs:boolean" tunnel="yes" />
              
<xsl:with-param name="local-cite-style" select="$local-cite-style" as="node()?" tunnel="yes" />
              
<xsl:with-param name="cite-ref" select="$cite-ref" as="node()" tunnel="yes" />
              
<xsl:with-param name="bib:source" select="." as="element()" tunnel="yes" />
            
</xsl:apply-templates>
          
</xsl:for-each>
        
</xsl:for-each-group>
        
<!-- FIX ME: config in CSL -->
        
<xsl:if test="position() != last()"></xsl:if>
      
</xsl:for-each-group>
  
</xsl:template>
  
<xsl:template match="db:citation" mode="sort_cited">
    XXXX
  
</xsl:template>
</xsl:stylesheet>






































v