XSLT - extending variable

S

Son KwonNam

The title is quite ambiguos.
Any way let me explain.

In java, the following is possible.
----
String str = "";
for (int i=0; i < 10; i++) {
str = str + i;
}
----
the str variable becomes "0123456789".

I tried that kind thing in XSLT. but failed.
---
<xsl:variable name="str1"/>
<xsl:variable name="str2"/>

<xsl:for-each select="/ROOT/ELEMENT">
<xsl:variable name="str1">
<xsl:value-of select="$str1"/>
<xsl:value-of select="@value1"/>
</xsl:variable>
<xsl:variable name="str2">
<xsl:value-of select="$str2"/>
<xsl:value-of select="@value1"/>
</xsl:variable>
</xsl:for-each>

<tr>
<td><xsl:value-of select="$str1"/></td>
<td><xsl:value-of select="$str2"/></td>
</tr>
---
Any other way to achieve this kind operation in XSLT?

--
** Son KwonNam
http://kr.blog.yahoo.com/kwon37xi

Please DO NOT reply to this message's email address.
The address is fake.
 
K

Kenneth Stephen

Son KwonNam said:
The title is quite ambiguos.
Any way let me explain.

In java, the following is possible.
----
String str = "";
for (int i=0; i < 10; i++) {
str = str + i;
}
----
Hi,

Try this :

<xsl:template name="functionalLanguagesImplementIterationViaRecursiveCalls">
<xsl:param name="counter" />
<xsl:if test = "$counter &gt;= 10">
<xsl:value-of select="$counter" />
<xsl:call-template
name="functionalLanguagesImplementIterationViaRecursiveCalls">
<xsl:with-param name="counter" select="number($counter + 1)" />
</xsl:call-template>
</xsl:if>
</xsl:template>

.....

and call this by saying :

<xsl:variable name="str">
<xsl:call-template
name="functionalLanguagesImplementIterationViaRecursiveCalls">
<xsl:with-param name="counter" select="number(0)" />
</xsl:call-template>
</xsl:variable>


Regards,
Kenneth
 
S

Son KwonNam

Thanks for your quick answer.
But what I really wanted is not just add continuous numbers.
The values to be added are strings.

If there is not something like "str = str + value" in XSLT,
I should use <xsl:for-each> more than two times.
And I really did - I use <xsl:for-each> 8 times for every <td>, It gives
me horrible performance.

Kenneth Stephen ¾´ ±Û:
Hi,

Try this :

<xsl:template name="functionalLanguagesImplementIterationViaRecursiveCalls">
<xsl:param name="counter" />
<xsl:if test = "$counter &gt;= 10">
<xsl:value-of select="$counter" />
<xsl:call-template
name="functionalLanguagesImplementIterationViaRecursiveCalls">
<xsl:with-param name="counter" select="number($counter + 1)" />
</xsl:call-template>
</xsl:if>
</xsl:template>

....

and call this by saying :

<xsl:variable name="str">
<xsl:call-template
name="functionalLanguagesImplementIterationViaRecursiveCalls">
<xsl:with-param name="counter" select="number(0)" />
</xsl:call-template>
</xsl:variable>


Regards,
Kenneth


--
** Son KwonNam
http://kr.blog.yahoo.com/kwon37xi

Please DO NOT reply to this message's email address.
The address is fake.
 
B

Ben Edgington

Son KwonNam said:
But what I really wanted is not just add continuous numbers.
The values to be added are strings.

If there is not something like "str = str + value" in XSLT,
I should use <xsl:for-each> more than two times.
And I really did - I use <xsl:for-each> 8 times for every <td>, It gives
me horrible performance.

This *can* be done recursively, you just have to carry around a
few extra parameters. Here's a solution that makes a node-set
($nodes) and passes it to the recursive template along with a
pointer that keeps track of the current position. It's not
pretty, but it works OK. As for performance, I'd be interested
to know how it compares.

This XML
- - -
<foo>
<item value1="one" value2="ONE"/>
<item value1="two" value2="TWO"/>
<item value1="three" value2="THREE"/>
<item value1="four" value2="FOUR"/>
</foo>
- - -

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

<xsl:template match="/foo">
<xsl:call-template name="concat-values">
<xsl:with-param name="num" select="1"/>
<xsl:with-param name="nodes" select="item"/>
<xsl:with-param name="string1" select="''"/>
<xsl:with-param name="string2" select="''"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="concat-values">
<xsl:param name="num"/>
<xsl:param name="nodes"/>
<xsl:param name="string1"/>
<xsl:param name="string2"/>
<xsl:choose>
<xsl:when test="$nodes[$num]">
<xsl:call-template name="concat-values">
<xsl:with-param name="num" select="$num + 1"/>
<xsl:with-param name="nodes" select="$nodes"/>
<xsl:with-param name="string1"
select="concat($string1,$nodes[$num]/@value1)"/>
<xsl:with-param name="string2"
select="concat($string2,$nodes[$num]/@value2)"/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:text>String1 = </xsl:text>
<xsl:value-of select="$string1"/>
<xsl:text>
String2 = </xsl:text>
<xsl:value-of select="$string2"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
- - -

gives this output
- - -
<?xml version="1.0"?>
String1 = onetwothreefour
String2 = ONETWOTHREEFOUR
- - -

which you should be able to adapt to your situation.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem in xml transformation using xslt 0
XSLT nightmare 2
XSLT Question 1
XSL Calculation 0
Unable to update variable 2
Slow running XSLT: Any help appreciated 5
XSLT incrementing count 2
XSLT question 4

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top