Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a xsl variable and assigning value to it

I'm working on an application which uses apache cocoon to convert an XML to PDF, and I'm redesigning the XSL that handles the input XML.

Currently in the XSL, we have code like this

<xsl:variable name="variable1">
   <xsl:choose>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'A'"/>
     </xsl:when>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'B'"/>
     </xsl:when>
   </xsl:choose>
</xsl:variable>

<xsl:variable name="variable2">
   <xsl:choose>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'X'"/>
     </xsl:when>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'Y'"/>
     </xsl:when>
   </xsl:choose>
</xsl:variable>

Will it work if I change it to this?

<xsl:variable name="variable1"/>
<xsl:variable name="variable2"/>
<xsl:choose>
   <xsl:when test="$testVariable ='1'">
         <xsl:variable name="variable1" select="'A'">        
         <xsl:variable name="variable2" select="'X'">
   </xsl:when> 
   <xsl:when test="$testVariable ='2'">
         <xsl:variable name="variable1" select="'B'">        
         <xsl:variable name="variable2" select="'Y'">
   </xsl:when> 
</xsl:choose>
like image 964
Ravi Shenoy Avatar asked Jan 30 '13 11:01

Ravi Shenoy


People also ask

How do you declare a variable and assign value in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

How do you declare a variable in XSLT?

XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables. The scope of the element is done by the element that contains it.

Can we reassign a value to variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.

How do you set a global variable in XSLT?

XSLT is a functional programming language, which, among other things means that there is no way one can change a variable. Variables in a functional programming language by definition are immutable.


1 Answers

No, unlike in a lot of other languages, XSLT variables cannot change their values after they are created. You can however, avoid extraneous code with a technique like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:variable name="mapping">
    <item key="1" v1="A" v2="B" />
    <item key="2" v1="X" v2="Y" />
  </xsl:variable>
  <xsl:variable name="mappingNode"
                select="document('')//xsl:variable[@name = 'mapping']" />

  <xsl:template match="....">
    <xsl:variable name="testVariable" select="'1'" />

    <xsl:variable name="values" select="$mappingNode/item[@key = $testVariable]" />

    <xsl:variable name="variable1" select="$values/@v1" />
    <xsl:variable name="variable2" select="$values/@v2" />
  </xsl:template>
</xsl:stylesheet>

In fact, once you've got the values variable, you may not even need separate variable1 and variable2 variables. You could just use $values/@v1 and $values/@v2 instead.

like image 134
JLRishe Avatar answered Sep 29 '22 23:09

JLRishe