473,769 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="countWage sGreaterZero">
<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:otherwis e>
<xsl:when test="$number[$index]/@Value > 0">
<xsl:choose>
<xsl:variable name="recursive _result">
<xsl:call-template name="countWage sGreaterZero">
<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="$recurs ive_result" />
</xsl:choose>
<xsl:otherwis e>
<xsl:variable name="recursive _result">
<xsl:call-template name="countWage sGreaterZero">
<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="$recurs ive_result" />
</xsl:otherwise>
</xsl:when>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Nov 12 '05 #1
4 3037
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()=$ind ex]

--
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()=$ind ex]

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

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:%2******** ********@tk2msf tngp13.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:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="ur n: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:n ode-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()=$ind ex]

--
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">S crewdriver</item>
<item price="29.99" quantity="10">H andsaw</item>
<item price="49.99" quantity="15">E lectric drill</item>
</items>

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="ur n: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:n ode-set($totals-rtf)/t"/>
Total: <xsl:value-of select="format-number(sum($tot als), '##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
1213
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 alternative? Thanks in advance, David
5
5304
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 adsq::QInitialise<struct adsq::Data>(struct adsq::Head<struct adsq::Data> *)" -----------adsq.h file
3
2144
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 VB-code. I've tried to translate the code to c# but I have problem with a small part of it ...
3
1344
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 runtime.
1
2642
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
2638
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
1506
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 fine in firefox, but in internet explorer: invalid argument. it says that about the makeColorSelector() now i can do window.top.anyOtherFunc(), so i think it has problems passing the node..
1
1760
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
1701
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 input parameter (ignore the rest since those are default) now in another class, I have:
1
1918
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 ‘bnode<cxsc::interval>::bnode(cxsc::interval*, cxsc::interval*, <unresolved overloaded function type>)’ node.cpp:38: note: candidates are: bnode<T>::bnode(const T*, const T*, T (*)(const T&, const T&)) node.cpp:34: note: ...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.