Reputation: 556
I just need to overwrite the variable in xsl
Example:
x=0
if x=0
then
x=3
I need to change the value of variable.
I am very new to xsl, please help me how to achieve this. This may be silly but I don't have any idea..
Upvotes: 4
Views: 10588
Reputation: 4580
As other comments have noted, variables in XSLT cannot be modified once they are set. The easiest way I've found to do this is to nest variables inside each other.
<xsl:variable name="initial_condition" select="VALUE"/>
Later
<xsl:variable name="modified_condition" select="$initial_condition + MODIFIER"/>
Some of our xsl has whole reams of nested calculations which really should be in the business logic which produces the source XML. Due to a period of time where there was no developer / time to add this business logic, it was added as part of the presentation layer.
It becomes extremely hard to maintain code like this, especially considering you've probably got control flow considerations to make. The variable names end up being very convoluted and readability drops through the floor. Code like this should be a last resort, it's not really what XSLT is designed for.
Upvotes: 3
Reputation: 660
The <xsl:variable>
in xslt
is not actual a variable. Which means it can not be changed after you have defined it and you can use it like this:
lets say we have this xml with name test.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<client-list>
<client>
<name>person1</name>
</client>
<client>
<name>person2</name>
</client>
<client>
<name>person3</name>
</client>
</client-list>
and we want to transform it to csv-like (comma separated values) but replacing the person1
with a hidden person with name person4
. Then lets say we have this xml with name test.xsl
which will be used to transform the test.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="hiddenname">person4</xsl:variable>
<!-- this template is for the root tag client-list of the test.xml -->
<xsl:template match="/client-list">
<!-- for each tag with name client you find, ... -->
<xsl:for-each select="client">
<!-- if the tag with name -name- don't have the value person1 just place its data, ... -->
<xsl:if test="name != 'person1'">
<xsl:value-of select="name"/>
</xsl:if>
<!-- if have the value person1 place the data from the hiddenperson -->
<xsl:if test="name = 'person1'">
<xsl:value-of select="$hiddenname"/>
</xsl:if>
<!-- and place a comma -->
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
the results will be
person4,person2,person3,
I hope this will help you.
Upvotes: 1
Reputation: 243519
I just need to overwrite the variable in xsl
Example x=0 if x=0 then x=3
XSLT is a functional language and among other things this means that a variable, once defined cannot be changed.
Certainly, this fact doesn't mean that a given problem cannot be solved using XSLT -- only that the solution doesn't contain any modifications of variable values, once defined.
Tell us what is your specific problem, and many people will be able to provide an XSLT solution :)
Upvotes: 3