[xsl] grouping/sorting again

Subject: [xsl] grouping/sorting again
From: Bruce D'Arcus <bdarcus@xxxxxxxxx>
Date: Fri, 29 Apr 2005 13:32:43 -0400
OK, I went back to the beginning and came up with the minmal example to test.

<?xml version="1.0" encoding="UTF-8"?>
<doc>
  <p><cite><ref href="1"/><ref href="2"/><ref href="4"/></cite></p>
  <p><cite><ref href="3"/><ref href="4"/><ref href="1"/></cite></p>
</doc>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
exclude-result-prefixes="xs" version="2.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="data">
    <biblist>
      <item id="1" author="one" year="1998"/>
      <item id="2" author="one" year="1998"/>
      <item id="3" author="two" year="2000"/>
      <item id="4" author="two" year="2001"/>
    </biblist>
  </xsl:variable>

  <xsl:template match="doc">
    <result>
      <xsl:apply-templates/>
    </result>
  </xsl:template>

  <xsl:template match="p">
    <para>
      <xsl:apply-templates/>
    </para>
  </xsl:template>

  <xsl:template match="cite">
    <xsl:variable name="citation" select="."/>
    <xsl:variable name="idref" select="ref/@href"/>
    <xsl:variable name="refs">
      <xsl:sequence select="$data/biblist/item[@id=$idref]"/>
    </xsl:variable>
    <xsl:variable name="temp-citation">
      <xsl:for-each-group select="$refs/item" group-by="@author">
        <xsl:for-each-group select="." group-adjacent="@year">
          <xsl:for-each select=".">
            <pos id="{@id}" author="{@author}">
              <xsl:value-of select="position()"/>
            </pos>
          </xsl:for-each>
        </xsl:for-each-group>
      </xsl:for-each-group>
    </xsl:variable>
    <citation>
      <ref>
        <raw>
          <xsl:copy-of select="$refs"/>
        </raw>
        <processed>
          <xsl:copy-of select="$temp-citation"/>
        </processed>
      </ref>
    </citation>
  </xsl:template>

</xsl:stylesheet>

Expected output:

<result>
     <para>
      <citation>
         <ref>
            <raw>
               <item id="1" author="one" year="1998"/>
               <item id="2" author="one" year="1998"/>
               <item id="4" author="two" year="2001"/>
            </raw>
            <processed>
               <pos id="1" author="one">1</pos>
               <pos id="2" author="one">2</pos>
               <pos id="4" author="two">1</pos>
            </processed>
         </ref>
      </citation>
   </para>
     <para>
      <citation>
         <ref>
            <raw>
               <item id="1" author="one" year="1998"/>
               <item id="3" author="two" year="2000"/>
               <item id="4" author="two" year="2001"/>
            </raw>
            <processed>
               <pos id="1" author="one">1</pos>
               <pos id="3" author="two">1</pos>
               <pos id="4" author="two">1</pos>
            </processed>
         </ref>
      </citation>
   </para>
</result>

Actual output:

<result>
     <para>
      <citation>
         <ref>
            <raw>
               <item id="1" author="one" year="1998"/>
               <item id="2" author="one" year="1998"/>
               <item id="4" author="two" year="2001"/>
            </raw>
            <processed>
               <pos id="1" author="one">1</pos>
               <pos id="4" author="two">1</pos>
            </processed>
         </ref>
      </citation>
   </para>
     <para>
      <citation>
         <ref>
            <raw>
               <item id="1" author="one" year="1998"/>
               <item id="3" author="two" year="2000"/>
               <item id="4" author="two" year="2001"/>
            </raw>
            <processed>
               <pos id="1" author="one">1</pos>
               <pos id="3" author="two">1</pos>
            </processed>
         </ref>
      </citation>
   </para>
</result>

Can someone please explain to me what I'm obviously not understanding
here?  I assume this is why I'm not getting expected output in the
realworld example I posted the other day.

Bruce

Current Thread