Assign <xsl: variable> after decompression

I use the method below to assign a value to a variable.

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

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

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

Is there any way to do this?


Here is the XML sample 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 that I need to get the latest <Vest_RecDate> for a specific <Vest_DocType> (say, SDD). And then I need to find in xml any date that is before <Vest_RecDate> this (same SDD).

If you then raise this particular section ( <VolLien> ) one and the last again. If I could reassign, I would put node and get the values ​​associated with this. Now I am doing this using a different loop. If there is something, I can avoid extrs loops.

+6
source share
3 answers

Not. XSLT variables are read-only. They cannot be assigned several times.

XSLT is not a required programming language, like, say, PHP. The reassignment of variables is impossible and not necessary.


EDIT: According to your comment:

My problem is that I need to get the latest version of <Vest_DocType> (say, SDD), and then I need to find in xml any date that is before <Vest_RecDate> 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> 
+8
source

Further addition to the previous answers: you are trying to solve a problem using low-level imperative methods that you learned from other programming languages. To help you solve the problem using a declarative approach consistent with XSLT, we need a description of the problem. Make it as high as possible - describe the output as a function of input, and not describe the computational steps that, in your opinion, are necessary for moving from one to another.

+4
source

Supplement to Tomalak's answer.

The variable is limited / local to the block in which it is declared and assigned a value (declaration and assignment occur immediately).

Suppose in the 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> 
+2
source

Source: https://habr.com/ru/post/910226/


All Articles