xml 声明一个 xsl 变量并为其赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14602884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:51:57  来源:igfitidea点击:

Declaring a xsl variable and assigning value to it

xmlxsltxpathapache-cocoon

提问by Ravi Shenoy

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.

我正在开发一个使用 apache cocoon 将 XML 转换为 PDF 的应用程序,我正在重新设计处理输入 XML 的 XSL。

Currently in the XSL, we have code like this

目前在 XSL 中,我们有这样的代码

<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>

回答by JLRishe

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:

不,与许多其他语言不同,XSLT 变量在创建后无法更改其值。但是,您可以使用以下技术避免无关代码:

<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 valuesvariable, you may not even need separate variable1and variable2variables. You could just use $values/@v1and $values/@v2instead.

事实上,一旦你得到了values变量,你甚至可能不需要单独的variable1variable2变量。你可以使用$values/@v1and$values/@v2代替。