Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT - extending variable

Reply
Thread Tools

XSLT - extending variable

 
 
Son KwonNam
Guest
Posts: n/a
 
      05-17-2004
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.
 
Reply With Quote
 
 
 
 
Kenneth Stephen
Guest
Posts: n/a
 
      05-17-2004

"Son KwonNam" <(E-Mail Removed)> wrote in message
news:c88u81$522$(E-Mail Removed)...
> 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="functionalLanguagesImplementIterationViaRecu rsiveCalls">
<xslaram name="counter" />
<xsl:if test = "$counter &gt;= 10">
<xsl:value-of select="$counter" />
<xsl:call-template
name="functionalLanguagesImplementIterationViaRecu rsiveCalls">
<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="functionalLanguagesImplementIterationViaRecu rsiveCalls">
<xsl:with-param name="counter" select="number(0)" />
</xsl:call-template>
</xsl:variable>


Regards,
Kenneth


 
Reply With Quote
 
 
 
 
Son KwonNam
Guest
Posts: n/a
 
      05-17-2004
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 :
> "Son KwonNam" <(E-Mail Removed)> wrote in message
> news:c88u81$522$(E-Mail Removed)...
>
>>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="functionalLanguagesImplementIterationViaRecu rsiveCalls">
> <xslaram name="counter" />
> <xsl:if test = "$counter &gt;= 10">
> <xsl:value-of select="$counter" />
> <xsl:call-template
> name="functionalLanguagesImplementIterationViaRecu rsiveCalls">
> <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="functionalLanguagesImplementIterationViaRecu rsiveCalls">
> <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.
 
Reply With Quote
 
Ben Edgington
Guest
Posts: n/a
 
      05-20-2004
Son KwonNam <(E-Mail Removed)> writes:
> 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"
xmlnssl="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">
<xslaram name="num"/>
<xslaram name="nodes"/>
<xslaram name="string1"/>
<xslaram 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>
<xsltherwise>
<xsl:text>String1 = </xsl:text>
<xsl:value-of select="$string1"/>
<xsl:text>&#x0a;String2 = </xsl:text>
<xsl:value-of select="$string2"/>
</xsltherwise>
</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


--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments