473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing node-sets as parameters?

I essentially need a countif() function for xsl. Something to where I could
do countif(node-set, condition). Rather than try to get too extreme, i
decided to just write one for my countif() with the condition hardcoded.
(this was also my first venture into creating "functions")

Pseudo-code is essentially this: Look at the current node and check the
condition. If the condition is true, call our function again with an
incremented count. If false, call function with the same value. Once we
reach the end of the node set, return the counter variable. The whole time
we're passing around a node-set and a current index.

My problem is that i get an error when i try to index into the node-set like
this $node-set[$index]. It looks like when i call the template and pass in
a node-set using the select attribute, it converts that node-set into a
string? (according to some posts i've read) So ways around that were to
use the mxsl:node-set(). The problem is i tried that and it didn't work.
(I'm using .NET to do the transform)

The other question I had was if i can even index into a node set like i'm
doing? Or maybe there's a better alternative for what i'm trying to do?

Thanks,
-A

-------- CODE
<xsl:template name="countWagesGreaterZero">
<xsl:param name="number" />
<xsl:param name="index" select="1"/>
<xsl:param name="count" select="0" />
<xsl:choose>
<xsl:when test="$index > count($number)">
<xsl:value-of select="$count" />
</xsl:when>
<xsl:otherwise>
<xsl:when test="$number[$index]/@Value > 0">
<xsl:choose>
<xsl:variable name="recursive_result">
<xsl:call-template name="countWagesGreaterZero">
<xsl:with-param name="number" select="$number" />
<xsl:with-param name="index" select="$index + 1" />
<xsl:with-param name="count" select="$count + 1" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$recursive_result" />
</xsl:choose>
<xsl:otherwise>
<xsl:variable name="recursive_result">
<xsl:call-template name="countWagesGreaterZero">
<xsl:with-param name="number" select="$number" />
<xsl:with-param name="index" select="$index + 1" />
<xsl:with-param name="count" select="$count" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$recursive_result" />
</xsl:otherwise>
</xsl:when>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Nov 12 '05 #1
4 3002
Alfred Taylor wrote:
I essentially need a countif() function for xsl. Something to where I could
do countif(node-set, condition). Rather than try to get too extreme, i
decided to just write one for my countif() with the condition hardcoded.
(this was also my first venture into creating "functions")
Well, are you sure you can't do that with count() function?
E.g. count(//employee[@wage>60000])

The problem usually is with sum() function when one needs to get sum of
calculated values. Then the simplest (but not the most effective)
solution is using temporary tree of calculated values and then coverting
it to nodeset using xxx:node-set function and summing. Other solution is
recursive template just like yours, but instead of passing index,
usually tail nodeset is passed (nodeset with no first node):
<xsl:with-param name="nodes" select="$nodes[position()>1]"/>
You may want to look at FXSL library where this ideas were developed
much further.
My problem is that i get an error when i try to index into the node-set like
this $node-set[$index].


Which error? Try $node-set[position()=$index]

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
Alfred Taylor wrote:
I essentially need a countif() function for xsl. Something to where I could
do countif(node-set, condition). Rather than try to get too extreme, i
decided to just write one for my countif() with the condition hardcoded.
(this was also my first venture into creating "functions")
Well, are you sure you can't do that with count() function?
E.g. count(//employee[@wage>60000])

The problem usually is with sum() function when one needs to get sum of
calculated values. Then the simplest (but not the most effective)
solution is using temporary tree of calculated values and then coverting
it to nodeset using xxx:node-set function and summing. Other solution is
recursive template just like yours, but instead of passing index,
usually tail nodeset is passed (nodeset with no first node):
<xsl:with-param name="nodes" select="$nodes[position()>1]"/>
You may want to look at FXSL library where this ideas were developed
much further.
My problem is that i get an error when i try to index into the node-set like
this $node-set[$index].


Which error? Try $node-set[position()=$index]

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #3

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Alfred Taylor wrote:
I essentially need a countif() function for xsl. Something to where I could do countif(node-set, condition). Rather than try to get too extreme, i
decided to just write one for my countif() with the condition hardcoded.
(this was also my first venture into creating "functions")
Well, are you sure you can't do that with count() function?
E.g. count(//employee[@wage>60000])


I guess countif() was a bad example to use. It's essentially a
countifAndSum() function. ;)

The problem usually is with sum() function when one needs to get sum of
calculated values. Then the simplest (but not the most effective)
solution is using temporary tree of calculated values and then coverting
it to nodeset using xxx:node-set function and summing. Other solution is
recursive template just like yours, but instead of passing index,
usually tail nodeset is passed (nodeset with no first node):
<xsl:with-param name="nodes" select="$nodes[position()>1]"/>
You may want to look at FXSL library where this ideas were developed
much further.
Ahh crap. Brings back bad memories of the days they taught me functional
languages. Man, looks like i'll have to get myself back into that mindset.
Thanks for reminding me on the different programming paradigms.

As for using xxx:node-set(), i'm still have a terrible time getting it to
work with the .NET transform. something as simple as this:

-- SNIP

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<xsl:call-template name="testMe">
<xsl:with-param name="param1" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="testMe">
<xsl:param name="param1" />
<xsl:value-of select="msxsl:node-set($param1)/Example/Element/@Value" />
</xsl:template>
</xsl:stylesheet>

-- END

I've read numerous other posts with people being able to use it
successfully, but i must be missing something here.

-A
My problem is that i get an error when i try to index into the node-set like this $node-set[$index].


Which error? Try $node-set[position()=$index]

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #4
Alfred Taylor wrote:
As for using xxx:node-set(), i'm still have a terrible time getting it to
work with the .NET transform. something as simple as this:


Here is a classical example of evaluating a sum of calculated values
using temporary tree and xxx:node-set() function. It's very easy, but
not the most effective way:

<items>
<item price="9.99" quantity="30">Screwdriver</item>
<item price="29.99" quantity="10">Handsaw</item>
<item price="49.99" quantity="15">Electric drill</item>
</items>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:template match="/">
<xsl:variable name="totals-rtf">
<xsl:for-each select="/items/item">
<t>
<xsl:value-of select="@price*@quantity"/>
</t>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="totals" select="msxsl:node-set($totals-rtf)/t"/>
Total: <xsl:value-of select="format-number(sum($totals), '##0.00')"/>
</xsl:template>
</xsl:stylesheet>

PS. rtf means result-tree fragment, which is temporary tree type in XSLT
1.0.
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: drs58 | last post by:
I'm calling a template that I'd like to pass a sort node set to. I'm creating the node set with <xsl:copy-of>...is there a way to sort the set prior to passing it? If not, what's a reasonable...
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
3
by: Hans [DiaGraphIT] | last post by:
Hi! Can anyone help me with translating a VB-code to C#? I'm reading the walkthrough: Passing data to a custom action. In part 4 were you create an installer class there is a example of...
3
by: ppcguy | last post by:
i've got this for IE: node.onchange = foo; but really i'd like to pass node object to foo function also. in html i would do onchange="foo(this)"...but this has to be done in javascript at...
1
by: lg2530 | last post by:
template<class T> struct NODE { T data; NODE<T> * next; }; template<typename T> NODE<T>* QuickSortList( NODE<T>* list ) {}
3
by: iskeletor | last post by:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define STUDENT_NUMBER 68 #define ARRAY_LENGTH 10 struct node{ char Name,Surname; int data,no;
1
by: jonavanmona | last post by:
Hi, im using a colorpicker script where i have to pass an element like this: node = targetDocument.getElementById('textcolorpicker'); var selector = window.top.makeColorSelector(node); works...
1
by: rajamani | last post by:
Hi dis my codig.. hav a luk.. #include<iostream> #include<cstring> using namespace std; template<class T>class List{
4
by: Akshay Loke | last post by:
Hi all, I have this function from a class MFnDagNode, addChild( MObject & child, unsigned int index = kNextPos, bool keepExistingParents = false ); which takes an object reference as first...
1
by: Ali | last post by:
The code at the end of this message works just fine with M$ VS2005 but with g++ 4.1.3 i get this: node.cpp: In function ‘int main()’: node.cpp:96: error: no matching function for call to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.