[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: Sat, 23 Apr 2005 16:35:04 -0400
xsl for each group position
Thanks for this Mike!

On Apr 23, 2005, at 10:32 AM, Michael Kay wrote:

[...snip...]

So this code:

   <xsl:variable name="author-position" select="position()"/>
   <xsl:variable name="shorten-author" as="xs:boolean"
select="$author-position > 1" />

sets $shorten-author to true if you're processing the second or
subsequent
group of same-author items. I don't think this is what you intended -
you
wanted it true for the second and subsequent items within each
same-author
group.

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:

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

Do you want $shorten-author to be true only for the second item, or
for the
third one as well?

The latter. The concrete example is this in a bibliography, assuming the "one" author from your example is in fact named "John Doe" (suitably marked up in XML):

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.

No, I need to know the position within both groups, and pass that as parameters for later processing. At least I assume this is the best approach (passing the parameters).

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.

Since this is what I in fact need, are you saying that basically I can't use two grouping levels and get what I'm after? I'm not quite following above.

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

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.