[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: grouping/position problem
Thanks for this Mike!
On Apr 23, 2005, at 10:32 AM, Michael Kay wrote: [...snip...] So this code: Exactly. (You're only executing this code once for each group of same-author items, so position() can't vary from one item in this group to another!) Yes, I figured this part out. Your data doesn't actually have an example of two items in which the author and year are the same, nor do you give the required output, so I'm now having to make guesses. Lets say you have:
Doe, John (2001a) ... . (2001b) ... . (2002) ... Doe, John and Jane Smith (2001) ... That's what I'm trying to handle here, where the same year values need to be, BTW, in the citations; e.g. in text you'd get (Doe 2001a) and so forth. What I'm currently getting is: Doe, John (2001a) ... . (2001b) ... Doe, John. (2002) ... Doe, John and Jane Smith (2001) I'm assuming only for the second: that is, you're concerned with the position within the group of same-author-same-year items. This is simply the value of position() within the innermost group.
If my assumption is wrong, and you want $shorten-author to be true for subsequent items with the same author whether or not the year is the same, you need to move the variable into the code that processes all the same-author items and groups them by year; you will also need to sort by year at this level so you get the position in the sorted order.
I couldn't figured out how to create an author-group position variable at that level, but then pass it through as a parameter at the inner level. P.S. I thought you solved this problem months ago? So did I! What happened is I realized recently that it wasn't working correctly. I'm not sure if that's because it did in the past and I broke something along the way, or if the kinds of examples I was running against it weren't exposing the problem. I also want to optimize performance, so am trying to cut out unnecessary processing. This part is crucial though. So I have everything else built around this that works correctly; it's just this little -- but crucial detail. And given that I need to ship off the book that is formatted with these stylesheets to production in the next week or so, I need to fix this ASAP! Hence the note of urgency you may have detected. Anyway, here's your simple doc that shows the issue: <list> <item author="one" year="2001"/> <item author="one" year="2001"/> <item author="one" year="2002"/> </list> ... here's the current example stylesheet: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <result> <xsl:apply-templates select="list" mode="sort_author-year"/> </result> </xsl:template> <xsl:template match="list" mode="sort_author-year"> <xsl:for-each-group select="item" group-by="@author"> <xsl:sort select="current-grouping-key()"/> <xsl:for-each-group select="current-group()" group-by="@year"> <xsl:sort select="current-grouping-key()"/> <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:for-each select="current-group()"> <xsl:variable name="author-position" select="position()"/> <xsl:variable name="shorten-author" as="xs:boolean" select="$author-position > 1"/> <xsl:apply-templates select="."> <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> </xsl:template> <xsl:template match="item"> <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> </item> </xsl:template> </xsl:stylesheet> ... here's the current result: <result> <item> <shorten-author>false</shorten-author> <year>2001a</year> </item> <item> <shorten-author>true</shorten-author> <year>2001a</year> </item> <item> <shorten-author>false</shorten-author> <year>2002b</year> </item> </result> ... here's the expected result: <result> <item> <shorten-author>false</shorten-author> <year>2001a</year> </item> <item> <shorten-author>true</shorten-author> <year>2001a</year> </item> <item> <shorten-author>true</shorten-author> <year>2002b</year> </item> </result> Bruce
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|