xsl-list
[Top] [All Lists]

Re: [xsl] Calculating cumulative values

2007-02-03 15:50:12
On 2/3/07, Andrew Welch <andrew(_dot_)j(_dot_)welch(_at_)gmail(_dot_)com> wrote:


You can just use XPath here, no need for recursion:

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


However evaluating this XPath expression repeatedly is O(N^2) and will
probably be slow for long lists.

Here is a solution, using the FXSL function

     f:scanl1()

This transformation:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:f="http://fxsl.sf.net/";
exclude-result-prefixes="f"

 <xsl:import href="../f/func-scanlDVC.xsl"/>
 <xsl:import href="../f/func-Operators.xsl"/>

 <!-- To be applied on testFunc-scanlDVC3.xml -->
 <xsl:output omit-xml-declaration="yes" indent="yes"/>


 <xsl:template match="node()|@*">
   <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="set">
   <xsl:copy>
     <xsl:copy-of select="@*"/>
     <xsl:for-each-group select="point" group-by="@x">
        <xsl:for-each select="f:scanl1(f:add(), current-group()/@y1)">
           <point x="{current-group()[1]/@x}" y="{.}"/>
        </xsl:for-each>
    </xsl:for-each-group>
   </xsl:copy>
</xsl:template>
</xsl:stylesheet>

When applied against the originally provided xml document:

<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>

produces the wanted result:

<root id="theroot">
  <set id="1">
     <point x="1" y="2"/>
     <point x="1" y="5"/>
     <point x="1" y="5"/>
     <point x="1" y="7"/>
     <point x="1" y="9"/>
     <point x="2" y="3"/>
     <point x="2" y="3"/>
     <point x="2" y="5"/>
     <point x="3" y="2"/>
     <point x="3" y="5"/>
     <point x="3" y="6"/>
     <point x="3" y="8"/>
     <point x="3" y="10"/>
  </set>
  <set id="2">
     <point x="1" y="2"/>
     <point x="1" y="5"/>
     <point x="1" y="5"/>
     <point x="1" y="7"/>
     <point x="2" y="2"/>
     <point x="3" y="2"/>
     <point x="3" y="4"/>
     <point x="3" y="6"/>
  </set>
  <set id="n">
     <point x="1" y="2"/>
     <point x="1" y="5"/>
     <point x="1" y="7"/>
     <point x="2" y="3"/>
     <point x="2" y="3"/>
     <point x="2" y="5"/>
     <point x="3" y="3"/>
  </set>
</root>


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--