xsl-list
[Top] [All Lists]

[xsl] Calculating cumulative values

2007-02-03 14:43:45
Hello,

Here is a tricky (to me) problem that requires looping over several sets and
calculating cumulative values (y2) from x and y1.  That is, for each x, set
y2 to be equal to the cumulative value of y1 as shown in the example output
below.

I made my best attempt with the stylesheet below, which I know is quite
incorrect but wanted to show I did actually try to solve the problem.  I am
now bogged down with the use of the variable element.

Any assistance would be greatly appreciated,

Simon

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

---------------------------------------------
Desired output
---------------------------------------------
<root id="theroot">
  <set id="1">
    <point x="1" y1="2" y2="2"/>
    <point x="1" y1="3" y2="5"/>
    <point x="1" y1="0" y2="5"/>
    <point x="1" y1="2" y2="7"/>
    <point x="1" y1="2" y2="9"/>
    <point x="2" y1="3" y2="3"/>
    <point x="2" y1="0" y2="3"/>
    <point x="2" y1="2" y2="5"/>
    <point x="3" y1="2" y2="2"/>
    <point x="3" y1="3" y2="5"/>
    <point x="3" y1="1" y2="6"/>
    <point x="3" y1="2" y2="8"/>
    <point x="3" y1="2" y2="10"/>
  </set>
  <set id="2">
    <point x="1" y1="2" y2="2"/>
    <point x="1" y1="3" y2="5"/>
    <point x="1" y1="0" y2="5"/>
    <point x="1" y1="2" y2="7"/>
    <point x="2" y1="2" y2="2"/>
    <point x="3" y1="2" y2="2"/>
    <point x="3" y1="2" y2="4"/>
    <point x="3" y1="2" y2="6"/>
  </set>
  <set id="n">
    <point x="1" y1="2" y2="2"/>
    <point x="1" y1="3" y2="5"/>
    <point x="1" y1="2" y2="7"/>
    <point x="2" y1="3" y2="3"/>
    <point x="2" y1="0" y2="3"/>
    <point x="2" y1="2" y2="5"/>
    <point x="3" y1="3" y2="3"/>
  </set>
</root>

---------------------------------------------
Prototype stylesheet - just to show I tried 
---------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:variable name="tempy" select="5"/>
<!-- Convert root and all elements tags 'as is'  -->
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="//point">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:for-each select="@y1">
      <xsl:attribute name="y2">
        <xsl:choose>
          <xsl:when test=". &gt; $tempy">
            <xsl:value-of select="$tempy * ."/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$tempy + ."/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>


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