XSLT - extending variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Son KwonNam

    XSLT - extending variable

    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:valu e-of select="$str1"/></td>
    <td><xsl:valu e-of select="$str2"/></td>
    </tr>
    ---
    Any other way to achieve this kind operation in XSLT?

    --
    ** Son KwonNam


    Please DO NOT reply to this message's email address.
    The address is fake.
  • Kenneth Stephen

    #2
    Re: XSLT - extending variable


    "Son KwonNam" <please@neverSe ndEmail.no> wrote in message
    news:c88u81$522 $1@news1.kornet .net...[color=blue]
    > 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;
    > }
    > ----[/color]
    Hi,

    Try this :

    <xsl:template name="functiona lLanguagesImple mentIterationVi aRecursiveCalls ">
    <xsl:param name="counter" />
    <xsl:if test = "$counter &gt;= 10">
    <xsl:value-of select="$counte r" />
    <xsl:call-template
    name="functiona lLanguagesImple mentIterationVi aRecursiveCalls ">
    <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="functiona lLanguagesImple mentIterationVi aRecursiveCalls ">
    <xsl:with-param name="counter" select="number( 0)" />
    </xsl:call-template>
    </xsl:variable>


    Regards,
    Kenneth


    Comment

    • Son KwonNam

      #3
      Re: XSLT - extending variable

      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 ¾´ ±Û:[color=blue]
      > "Son KwonNam" <please@neverSe ndEmail.no> wrote in message
      > news:c88u81$522 $1@news1.kornet .net...
      >[color=green]
      >>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;
      >>}
      >>----[/color]
      >
      > Hi,
      >
      > Try this :
      >
      > <xsl:template name="functiona lLanguagesImple mentIterationVi aRecursiveCalls ">
      > <xsl:param name="counter" />
      > <xsl:if test = "$counter &gt;= 10">
      > <xsl:value-of select="$counte r" />
      > <xsl:call-template
      > name="functiona lLanguagesImple mentIterationVi aRecursiveCalls ">
      > <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="functiona lLanguagesImple mentIterationVi aRecursiveCalls ">
      > <xsl:with-param name="counter" select="number( 0)" />
      > </xsl:call-template>
      > </xsl:variable>
      >
      >
      > Regards,
      > Kenneth
      >
      >[/color]


      --
      ** Son KwonNam


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

      Comment

      • Ben Edgington

        #4
        Re: XSLT - extending variable

        Son KwonNam <please@neverSe ndEmail.no> writes:[color=blue]
        > 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.[/color]

        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:styleshe et
        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:otherwis e>
        <xsl:text>Strin g1 = </xsl:text>
        <xsl:value-of select="$string 1"/>
        <xsl:text>&#x0a ;String2 = </xsl:text>
        <xsl:value-of select="$string 2"/>
        </xsl:otherwise>
        </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.

        Comment

        Working...