Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-20-2009, 01:27 PM   #1 (permalink)
techglider
Recruit
 
Join Date: Nov 2008
Posts: 9
techglider is on a distinguished road
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, 06:25 AM   #2 (permalink)
techglider
Recruit
 
Join Date: Nov 2008
Posts: 9
techglider is on a distinguished road
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?
techglider is offline   Reply With Quote
Old 02-23-2009, 06:46 AM   #3 (permalink)
techglider
Recruit
 
Join Date: Nov 2008
Posts: 9
techglider is on a distinguished road
What i am looking to do here is a running Total.
techglider is offline   Reply With Quote
Old 02-23-2009, 07:08 AM   #4 (permalink)
techglider
Recruit
 
Join Date: Nov 2008
Posts: 9
techglider is on a distinguished road
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, 10:27 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,706
sde is on a distinguished road
cool, thanks for posting the solution. it will also help people finding this thread when them come here searching.
__________________
Mike
sde is offline   Reply With Quote
Old 02-24-2009, 07:44 AM   #6 (permalink)
techglider
Recruit
 
Join Date: Nov 2008
Posts: 9
techglider is on a distinguished road
Quote:
Originally Posted by sde View Post
cool, thanks for posting the solution. it will also help people finding this thread when them come here searching.
No problem. Since global variables are similar to a final in java, their values are constant.
Therefore, you must get the present node and if its less than or equal to the previous node add it to the previous node. And since the first node will not have a sibling node before it, it is possible that the value may come back null so you might have to do an xsl:if to check whether (in this case 'amount' is null)

Code:
<xsl:if test="amount = ' ' ">
     <xsl:value-of select="amount" />
</xsl:if>
Something like that would if it if you are getting NaN for the first returned value.

Here is another example, a little different.

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:

EXSLT - Functions
techglider is offline   Reply With Quote
Old 02-26-2009, 11:43 PM   #7 (permalink)
cyberfriend
Recruit
 
Join Date: Feb 2009
Posts: 3
cyberfriend is on a distinguished road
Smile

You're not going to get it with xsl alone! Saxon has some good database integration (it uses Java).
I've done Saxon transforms that access databases. sweet stuff! Some of the other xsl engines do it too.
cyberfriend is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get element or attribute or tag from a WSDL. Rice Cai Java 0 02-26-2006 11:56 PM


All times are GMT -8. The time now is 10:34 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting