2013-03-19 45 views
0
<xsl:value-of select="$variable1"/> 
<xsl:value-of select="$variable2"/> 

變量1將是$ variable2的值。我該怎麼做呢?當變量的名稱是另一個變量的值時,如何引用XSLT變量

+1

你爲什麼要這樣做?當然有更好的方法來處理你的問題。請編輯問題並解釋你的問題。提供源XML文檔(一個小文檔)以及轉換的確切結果。另外,請說明轉換必須實施的任何規則/限制。 – 2013-03-19 04:07:34

回答

0

你不能。你可以做的是這樣的:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 

    <xsl:output method="xml" indent="yes"/> 

    <!-- Dictionary mapping names to values --> 
    <xsl:variable name="dict"> 
    <value name="var1">1</value> 
    <value name="var2">2</value> 
    </xsl:variable> 

    <xsl:template match="/"> 
    <!-- Name of the value in 'variable1' --> 
    <xsl:variable name="variable1" select="'var1'"/> 
    <!-- Get the value in 'variable2' using 'variable1' --> 
    <xsl:variable name="variable2" select="msxsl:node-set($dict)/value[@name=$variable1]"/> 
    <root> 
     <xsl:text>variable2: </xsl:text><xsl:value-of select="$variable2"/> 
    </root> 
    </xsl:template> 

</xsl:stylesheet> 

msxsl:node-set是Microsoft特定的擴展轉換片段節點集。如果您使用的是非Microsoft處理器,則應該有可用的等效功能。

+0

對於xsl參數是一樣的嗎? – user2184503 2013-03-19 01:57:46

+0

@ user2184503:是的,參數完全相同 – MiMo 2013-03-19 13:38:44

0

與大多數編程語言一樣,XSLT變量名在運行時不可訪問。

因此,請解釋您爲什麼要這麼做 - 您嘗試解決什麼問題 - 並且我們可以向您展示解決問題的正確方法。

相關問題