473,386 Members | 1,753 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.

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 2560

"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"> <xsl:variable name="test" select="0"/> </xsl:when>...
2
by: Norman Barker | last post by:
Hi, I have the following template <xsl:template match="midas:Request"> <xsl:variable name="var1" select="string(./midas:DataDescriptorGUID/@GUID)"/> <test> <result1><xsl:copy-of...
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 name="width" select="$max_image_width"/> </xsl:if>...
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 like to call an xsl:function, which returns a...
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 inserting a value into an additional xml-attribute. ...
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 more tags </Book>
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 syntax) I get error the variable "something" has...
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 description follows: What I want to do is to be...
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 node set of attributes which will include the...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.