Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2009, 09:31 PM   PM User | #1
TechGlider
New Coder

 
Join Date: Nov 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
TechGlider is an unknown quantity at this point
XSL Param or Attribute?

I need some help creating a dynamic variable..

I need to set up a variable that you can add to:

Can something like this be done --> This will be a for-each clause which will be looping through each set of node finding a child node, lets just say named = "amount"

so for example:

Code:
<account>
    <name />
    <account_number />
    <amount> x </amount>
</account>
Code:
<xsl:variable name="current_value">0</xsl:variable>

<xsl:for-each select="/account">
   
    <xsl:value-of select="$current_value + amount" /> <!-- i need this amount to be set to the current_value of this value --> 
         
</xsl:for-each>
What i need to is set up a dynamic variable like this " current_value = current_value + amount "

So every time it goes through and hits the node amount it adds it to the current_value?
TechGlider is offline   Reply With Quote
Old 02-23-2009, 02:29 PM   PM User | #2
TechGlider
New Coder

 
Join Date: Nov 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
TechGlider is an unknown quantity at this point
Ok so i found out you cannot alter the value of global values, so that theory is shot.. But no i am wondering if i can do something like this...

Code:
<xsl:value-of select="sum(AMOUNT[position() &lt;= current()]/AMOUNT)" />
Where i get the position of the previous node and make add it to the current node.. and i will just do a null check since the first node may get a null value?

What i am looking to do here is a running Total.

Last edited by TechGlider; 02-23-2009 at 02:50 PM..
TechGlider is offline   Reply With Quote
Old 02-23-2009, 03:12 PM   PM User | #3
TechGlider
New Coder

 
Join Date: Nov 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
TechGlider is an unknown quantity at this point
Figured it out.. For those of you interested on how to do a running total.... Here it is

Code:
//Declare the variable
<xsl:variable name="position" select="position()"/>

<xsl:value-of select="sum(//ACCOUNT[position() &lt;= $position]/AMOUNT)" />
TechGlider is offline   Reply With Quote
Old 02-23-2009, 03:59 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
this is a general implementation using exslt.

xml:
Code:
<?xml version="1.0"?>
<base>
  <item>
    <amount>1</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>2</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>3</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>4</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>5</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>6</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>7</amount>
    <check>yes</check>
  </item>
  <item>
    <amount>8</amount>
    <check>yes</check>
  </item>

</base>
xslt:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:f="http://exslt.org/functions"
		extension-element-prefixes="f">

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

  <xsl:template match="/">
    <values>
      <xsl:apply-templates select="*//amount"/>
      <sum>
	<xsl:value-of select="f:sum(*//amount)"/>
      </sum>
    </values>
  </xsl:template>

  <xsl:template match="amount">
    <val>
      <xsl:value-of select="."/>
    </val>
  </xsl:template>

  <f:function name="f:sum">
    <xsl:param name="mynodeset"/>
    <xsl:variable name="first"
		  select="$mynodeset[1]"/>
    <xsl:variable name="rest"
		  select="$mynodeset[position() > 1]"/>
    <xsl:choose>
      <xsl:when test="$rest">
	<f:result select="$first + f:sum($rest)"/>
      </xsl:when>
      <xsl:otherwise>
	<f:result select="$first"/>
      </xsl:otherwise>
    </xsl:choose>
  </f:function>

</xsl:stylesheet>
see also:

http://exslt.org/func/index.html

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.