XSLT - extending variable

Discussion in 'XML' started by Son KwonNam, May 17, 2004.

  1. Son KwonNam

    Son KwonNam Guest

    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.
     
    Son KwonNam, May 17, 2004
    #1
    1. Advertising

  2. "Son KwonNam" <> wrote in message
    news:c88u81$522$...
    > 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
     
    Kenneth Stephen, May 17, 2004
    #2
    1. Advertising

  3. Son KwonNam

    Son KwonNam Guest

    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" <> wrote in message
    > news:c88u81$522$...
    >
    >>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
    >
    >



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

    Please DO NOT reply to this message's email address.
    The address is fake.
     
    Son KwonNam, May 17, 2004
    #3
  4. Son KwonNam <> 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"
    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


    --
    Ben Edgington
    Mail to the address above is discarded.
    Mail to ben at that address might be read.
    http://www.edginet.org/
     
    Ben Edgington, May 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    737
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,732
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    648
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    733
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    702
Loading...

Share This Page