[xsl] Preceding Axis and Aggregation

Subject: [xsl] Preceding Axis and Aggregation
From: "Simon Shutter" <simon@xxxxxxxxxxx>
Date: Mon, 20 Aug 2007 12:38:31 -0700
I've been aggregating numbers in XSLT 1.0 using the preceding-sibling:: axis
for nodes with the same parent.  I now need to aggregate (for a given x) all
the values of y1 that preceed the context node even if they have different
parents.  For this I added another attribute using the preceding:: axis.
For my test data it appears to give the output I need.

If I am constrained to using an XSLT 1.0 processor is this the right
technique?

Thanks, Simon


---------------------------------------------
Stylesheet
---------------------------------------------
<xsl:template match="point">
 <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:attribute name="y2">
     <xsl:value-of select="sum(./@y1|preceding-sibling::point[@x =
current()/@x]/@y1)"/>
   </xsl:attribute>
   <xsl:attribute name="y3">
     <xsl:value-of select="sum(./@y1|preceding::point[@x =
current()/@x]/@y1)"/>
   </xsl:attribute>
 </xsl:copy>
</xsl:template>

---------------------------------------------
Input
---------------------------------------------

<root id="theroot">
  <set id="1">
    <point x="1" y1="2" />
    <point x="1" y1="3" />
    <point x="1" y1="0" />
    <point x="1" y1="2" />
    <point x="1" y1="2" />
    <point x="2" y1="3" />
    <point x="2" y1="0" />
    <point x="2" y1="2" />
    <point x="3" y1="2" />
    <point x="3" y1="3" />
    <point x="3" y1="1" />
    <point x="3" y1="2" />
    <point x="3" y1="2" />
  </set>
  <set id="2">
    <point x="1" y1="2" />
    <point x="1" y1="3" />
    <point x="1" y1="0" />
    <point x="1" y1="2" />
    <point x="2" y1="2" />
    <point x="3" y1="2" />
    <point x="3" y1="2" />
    <point x="3" y1="2" />
  </set>
  <set id="n">
    <point x="1" y1="2" />
    <point x="1" y1="3" />
    <point x="1" y1="2" />
    <point x="2" y1="3" />
    <point x="2" y1="0" />
    <point x="2" y1="2" />
    <point x="3" y1="3" />
  </set>
</root>

---------------------------------------------
Output
---------------------------------------------


    <point x="1" y1="2" y2="2" y3="2" />
    <point x="1" y1="3" y2="5" y3="5" />
    <point x="1" y1="0" y2="5" y3="5" />
    <point x="1" y1="2" y2="7" y3="7" />
    <point x="1" y1="2" y2="9" y3="9" />
    <point x="2" y1="3" y2="3" y3="3" />
    <point x="2" y1="0" y2="3" y3="3" />
    <point x="2" y1="2" y2="5" y3="5" />
    <point x="3" y1="2" y2="2" y3="2" />
    <point x="3" y1="3" y2="5" y3="5" />
    <point x="3" y1="1" y2="6" y3="6" />
    <point x="3" y1="2" y2="8" y3="8" />
    <point x="3" y1="2" y2="10" y3="10" />
  
  
    <point x="1" y1="2" y2="2" y3="11" />
    <point x="1" y1="3" y2="5" y3="14" />
    <point x="1" y1="0" y2="5" y3="14" />
    <point x="1" y1="2" y2="7" y3="16" />
    <point x="2" y1="2" y2="2" y3="7" />
    <point x="3" y1="2" y2="2" y3="12" />
    <point x="3" y1="2" y2="4" y3="14" />
    <point x="3" y1="2" y2="6" y3="16" />
  
  
    <point x="1" y1="2" y2="2" y3="18" />
    <point x="1" y1="3" y2="5" y3="21" />
    <point x="1" y1="2" y2="7" y3="23" />
    <point x="2" y1="3" y2="3" y3="10" />
    <point x="2" y1="0" y2="3" y3="10" />
    <point x="2" y1="2" y2="5" y3="12" />
    <point x="3" y1="3" y2="3" y3="19" />

Current Thread