[xsl] current-group()[1] within xsl:for-each-group

Subject: [xsl] current-group()[1] within xsl:for-each-group
From: Kevin Rodgers <kevin.rodgers@xxxxxxx>
Date: Tue, 1 Mar 2005 19:15:29 -0700
This is a much harder problem than my previous question :-/

I've got this in a template:

      <xsl:for-each-group select="mb3e:document" group-by="mb3e:fam_id">
        <xsl:sort select="mb3e:prim_sort_key"/>
        <xsl:variable name="first-structured-number"
        <xsl:apply-templates mode="mil" select="current-group()">
          <xsl:with-param name="family-structured-number"
          <xsl:sort select="mb3e:date_list/mb3e:date[@type='PUBL']"

The idea is to process the mb3e:document elements in groups, which are
defined as having mb3e:fam_id subelements with the same content.  And
within each group, to process them in reverse order of their publication
date (in YYYY-MM-DD format), in each case using the result of calling
the esd:structured-number function on the most recent document.  (The
groups themselves are processed according to the mb3e:prim_sort_key

The esd:structured-number function is slow, so I only want to call it
once per group.  Actually, the template that is applied within the
xsl:for-each-group instruction has to call it for each mb3e:document
element, but I want to avoid calling it more than once for any of those
elements (including the first, i.e. most recent):

  <xsl:variable name="structured-number"
                select="if (position() = 1)
                        then $family-structured-number
                        else esd:structured-number(.)"/>

I think this works most of the time, but sometimes the
family-structured-number parameter value (i.e. $first-structured-number)
is not the value that would be returned if the esd:structured-number
function were called on the most recent mb3e:document element in the
group.  For example, in once case it was the 7th most recent of a group
of 58.

Is there something inherently wrong with the code above?  I know from
the output that the mb3e:document elements are grouped and sorted as I
intend.  The output also indicates that the postition() = 1 test in the
mb3e:document template is working.  But for some reason the
current-group()[1] expression within the xsl:for-each-group instruction
isn't always returning the element I expect.

Just in case, here's an abbreviated example showing all the referenced
elements and attributes above:

<prim_sort_key>MIL PRF 0000001</prim_sort_key>
<date type="PUBL">2003-06-11</date>
<date type="PUBL_MOD">2003-06-17</date>
<date type="IHS_MOD">2003-07-22</date>

Kevin Rodgers

Current Thread