[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Calculating cumulative values

Subject: Re: Calculating cumulative values
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Sat, 3 Feb 2007 21:58:32 +0000
Re:  Calculating cumulative values
On 2/3/07, Simon Shutter <simon@xxxxxxxxxxx> wrote:
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>

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[@x =
current()/@x]/@y1)"/>
  </xsl:attribute>
</xsl:copy>
</xsl:template>

cheers
andrew

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.