472,332 Members | 1,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

xsl:variable question

Hello

Given a node set, I try to compute the total of the string-length
value of each node.

For instance, with :

<xsl:for-each select="//q">
<!-- the length of each node is compute with: -->
<xsl:value-of select="string-length(.)" />
</xsl:for-each>

but how to get the total of the different values, since xsl:variable
do not allow the modification of its value?

Thanks,
Sylvain
Jul 20 '05 #1
4 2487

"sylvain.loiseau" <sy*************@wanadoo.fr> wrote in message
news:c1**********@news-reader2.wanadoo.fr...
Hello

Given a node set, I try to compute the total of the string-length
value of each node.

For instance, with :

<xsl:for-each select="//q">
<!-- the length of each node is compute with: -->
<xsl:value-of select="string-length(.)" />
</xsl:for-each>

but how to get the total of the different values, since xsl:variable
do not allow the modification of its value?


Copy all nodes into an the RTF contents of an xsl:variable, then just use
the string-length() function:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:variable name="vPersons">
<xsl:copy-of select="//person"/>
</xsl:variable>

<xsl:value-of select="string-length($vPersons)"/>
</xsl:template>
</xsl:stylesheet>

when this transformation is applied to the following source.xml:

<persons>
<person name="pt" public="false">
<password>asdf</password>
</person>
<person name="ab" public="true">xx</person>
</persons>

the wanted result is produced:

6
Hope this helped.
Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
Jul 20 '05 #2
> Hope this helped.

Yes it helped. Thanks !

Sylvain
Jul 20 '05 #3
"sylvain.loiseau" <sy*************@wanadoo.fr> wrote in message news:<c1**********@news-reader5.wanadoo.fr>...
Hope this helped.


Yes it helped. Thanks !

Sylvain

Another way to do this is by using the Tree Visitor pattern:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()" name="treeVisitor">
<xsl:param name="ptxtLength" select="0"/>
<xsl:variable name="vsubtreeLength">
<xsl:choose>
<xsl:when test="not(node())">0</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()[1]">
<xsl:with-param name="ptxtLength"
select="$ptxtLength"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:choose>
<xsl:when test="following-sibling::node()">
<xsl:apply-templates select="following-sibling::node()[1]">
<xsl:with-param name="ptxtLength"
select="$ptxtLength + $vsubtreeLength * (number(.) = number(.))"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$vsubtreeLength"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="node()[not(node()) and not(following-sibling::node())]">
<xsl:param name="ptxtLength" select="0"/>
<xsl:value-of select="$ptxtLength"/>
</xsl:template>

<xsl:template match="person">
<xsl:param name="ptxtLength" select="0"/>

<xsl:call-template name="treeVisitor">
<xsl:with-param name="ptxtLength"
select="$ptxtLength + string-length(.)"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>

When this transformation is applied on this source.xml:

<persons>
<person name="pt" public="false">
<password>asdf</password>
</person>
<person name="ab" public="true">xx</person>
</persons>

the wanted result is obtained:

6
Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
Jul 20 '05 #4

"sylvain.loiseau" <sy*************@wanadoo.fr> wrote in message
news:c1**********@news-reader2.wanadoo.fr...
Hello

Given a node set, I try to compute the total of the string-length
value of each node.

For instance, with :

<xsl:for-each select="//q">
<!-- the length of each node is compute with: -->
<xsl:value-of select="string-length(.)" />
</xsl:for-each>

Hi,

I've looked at the other solutions presented and I feel that even though
they work, they really arent utilizing the functional programming paradigm.
Here is my solution :

<xsl:call-template name="accumulate">
<xsl:with-param name="nodes" select="//q" />
<xsl:with-param name="sum" select="0" />
</xsl:call-template>

.....and then define "accumulate" as :

<xsl:template name="accumulate">
<xsl:param name="nodes" />
<xsl:param name="sum" />

<xsl:choose>
<xsl:when test = "count($nodes) &gt; 1">
<xsl:call-template name="accumulate">
<xsl:with-param name="nodes" select="$nodes[position() != 1]" />
<xsl:with-param name="sum" select="$sum + string-length($nodes[1])" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum + string-length($nodes[1])" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Regards,
Kenneth
Jul 20 '05 #5

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

Similar topics

1
by: Xeon | last post by:
Hi, I'm trying this code snippet below but the parser (sablotron) returns error : <xsl:choose> <xsl:when test="some test case">...
2
by: Norman Barker | last post by:
Hi, I have the following template <xsl:template match="midas:Request"> <xsl:variable name="var1"...
10
by: Tjerk Wolterink | last post by:
The following code does not work: <xsl:variable name="width" select="form:image-width"/> <xsl:if test="$width>$max_image_width"> <xsl:variable...
1
by: schaf | last post by:
Hello NG ! I have a big problem. I would like to go through a xml file in a xsl:for-each statement. for-each entry (ID) in the XML file i would...
3
by: schaf | last post by:
Hi ! I have a little question. With my XSL File I add additional XML-Tags to an existing XML-File. But I would like to use a xsl:variable for...
2
by: sucheta.phatak | last post by:
Hello, I am trying to use XSLT. Here is the problem that I am facing. My XML file: <Book level="1" name="Mapplicationtoc1"> ....... Some...
5
by: Afshar Mohebbi | last post by:
Hi everybody there, I've declared a <xsl:variable name="something" select="999"/> in my xslt and when I want to set another value in it (with same...
2
by: jobooker | last post by:
I'm having issues sorting. The short description is, how do I set the select attribute of xsl:sort to be the value of an xsl:variable? The longer...
0
by: Markchivs | last post by:
Hi all, I wonder if anyone can shed any light on this problem for me? In the following code I'm trying to set a variable called 'atts' with a...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.