Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)" />

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •