[xml] 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>

This question is related to xml xslt xpath apache-cocoon

The answer is


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.


Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to xslt

Generic XSLT Search and Replace template Concatenate multiple node values in xpath XSL if: test with multiple test conditions Error: The processing instruction target matching "[xX][mM][lL]" is not allowed Weird behavior of the != XPath operator Declaring a xsl variable and assigning value to it How to implement if-else statement in XSLT? How do I specify "not equals to" when comparing strings in an XSLT <xsl:if>? How to concat a string to xsl:value-of select="...? XPath with multiple conditions

Examples related to xpath

Xpath: select div that contains class AND whose specific child element contains text XPath: difference between dot and text() How to set "value" to input web element using selenium? How to click a href link using Selenium XPath: Get parent node from child node What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing? How to use XPath preceding-sibling correctly Selenium and xPath - locating a link by containing text How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug? Concatenate multiple node values in xpath

Examples related to apache-cocoon

Declaring a xsl variable and assigning value to it