Altova Mailing List Archives

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

From: Kevin Rodgers <kevin.rodgers@------->
Date: 3/2/2005 2:16:00 AM
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


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.