[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: grouping/position problem

Subject: Re: grouping/position problem
From: Bruce D'Arcus <bdarcus@xxxxxxxxx>
Date: Sun, 24 Apr 2005 10:00:16 -0400
xsl and param and position
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.