Declaring a xsl variable and assigning value to it

前端 未结 1 1046
谎友^
谎友^ 2020-12-28 08:23

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 h

相关标签:
1条回答
  • 2020-12-28 09:15

    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.

    0 讨论(0)
提交回复
热议问题