6

I am using the below way to assign value to a variable.

<xsl:variable name="NewValue">
  <xsl:value-of select="normalize-space(//root/id/amount)"/>
</xsl:variable>

After the assignment I want to assign new value to the same variable. Like this:-

<xsl:variable name="NewValue" select="normalize-space(//root/id/amountnew)">

Is there any way for this?


Here the sample of XML that I have:

<VolLien>
  <Vest_DocType>SDD</Vest_DocType>
  <Vest_Instrument>395072</Vest_Instrument>
  <Vest_OfOfficialEntity>eee</Vest_OfOfficialEntity>
  <Vest_RecDate>12/24/2009</Vest_RecDate>
  <Vest_Grantee1>abc dd</Vest_Grantee1>
  <Vest_Grantor1>sss</Vest_Grantor1>
  <Vest_RejectCode />
  <Vest_RejectReason /> 
  <Vest_ImageNum> </Vest_ImageNum>
</VolLien>

My problem is I need to get latest <Vest_RecDate> of particular <Vest_DocType>(say SDD) And then I need to search across the xml any date which is before <Vest_RecDate> of this(same SDD).

If then raise that particular section(<VolLien>) alone and again latest. If I could have reassignment I would position of the node and get the values associated to that. Now I am doing this using another loop. If something is there I can avoid extrs loops.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Varun
  • 426
  • 3
  • 7
  • 18
  • Please add a sample of the XML you are working on, along with a short description of what you're trying to do. – Tomalak Mar 08 '12 at 06:40
  • Hi, I was doing some rules manipulation in xsl with my xml data. For a certain scenario I wanted to reassign another value to the variable that is already declared. This is reason I looked for any option like this,, Anyway Thank You for you quick respone and valueble input – Varun Mar 08 '12 at 07:02
  • @Varun I have did not fully understand your requirements, but have a look at my modified answer, maybe this gets you started. *(PS: You can remove the comments, as I moved them to the question.)* – Tomalak Mar 08 '12 at 15:57

3 Answers3

7

No. XSLT variables are read-only. They cannot be assigned multiple times.

XSLT is not an imperative programming language like, say, PHP. Reassigning variables is both impossible and unnecessary.


EDIT: According to your comment:

My problem is I need to get latest of particular <Vest_DocType> (say SDD) and then I need to search across the xml any date which is before <Vest_RecDate> of this (same SDD).

here is an XSLT snippet that can do this for you:

<!-- a key to retrieve all <VolLien> nodes of a particular DocType -->
<xsl:key name="VolLien-by-DocType" match="VolLien" use="Vest_DocType" />

<xsl:template match="/">
  <xsl:call-template name="latest-VolLien">
    <xsl:with-param name="DocType" select="'SDD'" />
  </xsl:call-template>
</xsl:template>

<!-- this template processes specified <VolLien> nodes in the right order -->
<xsl:template name="latest-VolLien">
  <xsl:param name="DocType" select="''" />

  <xsl:for-each select="key('VolLien-by-DocType', $DocType)">
    <!-- sorting is a little complicated because you 
         use dd/mm/yyyy instead of yyyymmdd -->
    <xsl:sort 
      select="
        number(substring(Vest_RecDate, 7, 4)) + 
        number(substring(Vest_RecDate, 4, 2)) + 
        number(substring(Vest_RecDate, 1, 2))
      "
      data-type="number"
    />
    <!-- do something with last <VolLien> (in date order) -->
    <xsl:if test="position() = last()">
      <xsl:apply-templates select="." />
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template match="VolLien">
  <!-- this template will actually process the nodes,
       do whatever you want here -->
</xsl:template>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
4

Further addition to previous answers: you're trying to solve some problem using low-level imperative techniques that you have learnt from other programming languages. To help you solve the problem using the declarative approach appropriate to XSLT, we will need a description of the problem. Make it as high level as possible - describe the output as a function of the input, rather than describing the computational steps you think are needed to get from one to the other.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
2

Addition to Tomalak's answer.

A variable is limited/local to a block in which it is declared and assigned with value (declaration and assignment happen at once).

Suppose in example:

  <xsl:template match="Node1">
    <xsl:variable name="test" select="'test_value'"/>
    <xsl:for-each select="foo">
      <xsl:variable name="var1" select="."/>
      <xsl:value-of select="$var1"/>
    </xsl:for-each>
    <xsl:for-each select="bar">
      <xsl:value-of select="$var1"/>
      <!--The variable "$var1" is out of scope.. So above statement is wrong-->
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="Node2">
    <xsl:value-of select="$test"/>
    <!--The variable "$test" is out of scope.. So above statement is wrong too!-->
  </xsl:template>
Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114