Topic
  • 3 replies
  • Latest Post - ‏2013-11-01T17:55:17Z by HermannSW
samuels
samuels
64 Posts

Pinned topic compare and change varibale value in xslt

‏2013-10-31T16:02:27Z |

helllo experts,

how to change the variable value in xslt?

for example variable var1 has value "abc" and now there will be call to the serivce and the service response is stored in variable var2 as "def". now we have to compare var1 and var2,if they both are different var1 should be replaced with the value var2.

sample example:

<xsl:varibale name="var1" value="abc"/>

<xsl:varibale name="response">

<dp:url-open("targetip")>

        <xsl:value-of select="$var1"/>

</dp:url-open>

<xsl:variable name="var2">

 <xsl:value-of select="$response/alphabet">

</xsl:variable>

now we have to compare var2 with var1 and replace the value of var1 with var2 if they both are not same. and i need to cache that variable. can we cache the variable in datapower

Updated on 2013-10-31T16:04:38Z at 2013-10-31T16:04:38Z by samuels
  • NILAY97
    NILAY97
    286 Posts

    Re: compare and change varibale value in xslt

    ‏2013-10-31T18:26:12Z  

    there are lot many examples available on NET which can tell you how to compare the values stored in a variable.

    Regarding caching the variable, the XML Manager would cache the stylesheet and ofcourse its contents.

     

    Thanks,

    Nilay

  • ted.jump
    ted.jump
    473 Posts

    Re: compare and change varibale value in xslt

    ‏2013-10-31T20:39:03Z  

    Well, in XSLT you can't change the value of a variable once it is created.

    What you can do is build the variable in a more complex fashion, around a <xsl:choose> construct, using a temporary value to begin with.

    Off the cuff it would look similar to this (not compiled, so YMMV)

    <xsl:variable name="temp" value="'yadda'"/>

    <xsl:variable name="final">

    <xsl:choose>

    <xsl:when test="$temp = 'onevalue'">

    <xsl:text>final var value for onevalue</xsl:text>

    </xsl:when>

    <xsl:otherwise>

    <xsl:text>other var value when not onevalue</xsl:text>

    </xsl:othewise>

    </xsl:choose>

    </xsl:variable>

    your <xsl:choose> construct could be quite complex, and your tests be quite fancy, if necessary.

    ----

    random though, if you are doing something based upon what is essentially a table lookup you could define a different var with the node-set that defines the table, and use the xpath lookup to get the value to set your final var value to.

    ----

    good luck.

    oh, you can change dp:variable values willy-nilly, I just don't know that it's a good idea. It "smells" to me to be more costly than the above construct, but I don't really know.

  • HermannSW
    HermannSW
    6215 Posts

    Re: compare and change varibale value in xslt

    ‏2013-11-01T17:55:17Z  
    • ted.jump
    • ‏2013-10-31T20:39:03Z

    Well, in XSLT you can't change the value of a variable once it is created.

    What you can do is build the variable in a more complex fashion, around a <xsl:choose> construct, using a temporary value to begin with.

    Off the cuff it would look similar to this (not compiled, so YMMV)

    <xsl:variable name="temp" value="'yadda'"/>

    <xsl:variable name="final">

    <xsl:choose>

    <xsl:when test="$temp = 'onevalue'">

    <xsl:text>final var value for onevalue</xsl:text>

    </xsl:when>

    <xsl:otherwise>

    <xsl:text>other var value when not onevalue</xsl:text>

    </xsl:othewise>

    </xsl:choose>

    </xsl:variable>

    your <xsl:choose> construct could be quite complex, and your tests be quite fancy, if necessary.

    ----

    random though, if you are doing something based upon what is essentially a table lookup you could define a different var with the node-set that defines the table, and use the xpath lookup to get the value to set your final var value to.

    ----

    good luck.

    oh, you can change dp:variable values willy-nilly, I just don't know that it's a good idea. It "smells" to me to be more costly than the above construct, but I don't really know.

    > oh, you can change dp:variable values willy-nilly, I just don't know that it's a good idea.
    >
    It is not -- if using dp:variable() just because needing to overwrite a value -- the correct (functional) XSLT constructs should be used.
     

    >It "smells" to me to be more costly than the above construct, but I don't really know.
    >
    Yes, I do compare "dp:variable() access vs. $variable access" with "harddisk access vs. main memory access".

    It is a best practice NOT to use the same dp:variable() in several places.
    Instead the  dp:variable()  should be "cached/stored" in a XSLT variable and that should be used in several places like:

    <xsl:variable name="URL-in" select="dp:variable('var://service/URL-in')"/>
    ...
    ... $URL-in ...
    ...
    <xsl:if test="contains($URL-in,...)">
    ...


    Hermann<myXsltBlog/> <myXsltTweets/> <myCE/>