xsl-list
[Top] [All Lists]

Re: grouping/position problem

2005-04-24 07:00:16

On Apr 24, 2005, at 9:10 AM, Michael Kay wrote:

In effect you need to sort by year before you group by year, and set
shorten-author to true for all but the first item in the list of items for
an author in year order.

Something like:

Thank you, thank you, thank you Mike!  That (finally!) works!

Quick question, though: what is the group-adjacent='@year' doing that's different than group-by I was using?

Below is the final input, stylesheet and output for reference (and the archives), and my plan is to use that to generate the enhanced data and hold it in a global variable.

<list>
   <item author="one" year="2001"/>
   <item author="one" year="2001"/>
   <item author="one" year="2002"/>
</list>

===

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"
  xmlns:bib="http://purl.org/NET/xbiblio/citeproc";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="bib xs">

   <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

   <xsl:param name="sort_order" select="'author-year'"/>

   <xsl:template match="/">
     <xsl:apply-templates select="$enhanced-biblist/result"/>
   </xsl:template>

   <xsl:variable name="enhanced-biblist">
     <xsl:choose>
       <xsl:when test="$sort_order='citekey'">
         <xsl:apply-templates select="list" mode="sort_citekey"/>
       </xsl:when>
       <xsl:when test="$sort_order='cited'">
         <xsl:apply-templates select="list" mode="sort_cited"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:apply-templates select="list" mode="sort_author-year"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:variable>

   <xsl:template match="list" mode="sort_author-year">
    <result>
       <xsl:for-each-group select="item" group-by="@author">
         <xsl:sort select="current-grouping-key()"/>
<xsl:variable name="items-for-author-sorted-by-year" as="element(item)*">
           <xsl:perform-sort select="current-group()">
             <xsl:sort select="@year"/>
           </xsl:perform-sort>
         </xsl:variable>
         <xsl:variable name="first-item-for-author" as="element(item)"
            select="$items-for-author-sorted-by-year[1]"/>
<xsl:for-each-group select="$items-for-author-sorted-by-year" group-adjacent="@year">
           <xsl:for-each select="current-group()">
             <xsl:variable name="year">
               <xsl:value-of select="current-grouping-key()"/>
               <xsl:if test="last() > 1">
                 <xsl:number value="position()" format="a"/>
               </xsl:if>
             </xsl:variable>
             <xsl:variable name="shorten-author" as="xs:boolean"
                 select="not(. is $first-item-for-author)" />
             <xsl:apply-templates select="." mode="enhance">
               <xsl:with-param name="year" select="$year"/>
<xsl:with-param name="shorten-author" select="$shorten-author"/>
             </xsl:apply-templates>
           </xsl:for-each>
         </xsl:for-each-group>
       </xsl:for-each-group>
     </result>
   </xsl:template>

   <xsl:template match="item" mode="enhance">
     <xsl:param name="shorten-author"/>
     <xsl:param name="year"/>
     <item>
       <shorten-author>
         <xsl:value-of select="$shorten-author"/>
       </shorten-author>
       <year>
         <xsl:value-of select="$year"/>
       </year>
       <author>
         <xsl:value-of select="@author"/>
       </author>
     </item>
    </xsl:template>

   <xsl:template match="result">
     <xsl:param name="shorten-author"/>
     <xsl:param name="year"/>
     <biblist>
       <xsl:apply-templates>
         <xsl:with-param name="year" select="$year"/>
<xsl:with-param name="shorten-author" select="$shorten-author"/>
       </xsl:apply-templates>
     </biblist>
    </xsl:template>

   <xsl:template match="item">
     <xsl:param name="shorten-author"/>
     <xsl:param name="year"/>
     <xsl:copy-of select="."/>
   </xsl:template>

</xsl:stylesheet>

===

<?xml version="1.0" encoding="UTF-8"?>
<biblist>
   <item>
      <shorten-author>false</shorten-author>
      <year>2001a</year>
      <author>one</author>
   </item>
   <item>
      <shorten-author>true</shorten-author>
      <year>2001b</year>
      <author>one</author>
   </item>
   <item>
      <shorten-author>true</shorten-author>
      <year>2002</year>
      <author>one</author>
   </item>
</biblist>

Execution time: 41 milliseconds


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>