這是可能的whenthe XSLT樣式表訪問本身作爲一個普通的XML文檔:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="p1" select="'P1-Value'"/>
<xsl:param name="p2" select="'P2-Value'"/>
<xsl:param name="p3" select="'P3-Value'"/>
<xsl:param name="pName" select="'p3'"/>
<xsl:param name="pNumber" select="2"/>
<xsl:variable name="vDoc" select="document('')"/>
<xsl:template match="/">
<xsl:value-of select=
"concat('Param with name ',
$pName,
' has value: ',
$vDoc/*/xsl:param[@name = $pName]/@select
)"/>
<xsl:text>
</xsl:text>
<xsl:variable name="vParam" select=
"$vDoc/*/xsl:param[@name = concat('p', $pNumber)]"/>
<xsl:value-of select=
"concat('Param with name p',
$pNumber,
' has value: ',
$vParam/@select
)"/>
</xsl:template>
</xsl:stylesheet>
產生想要的結果:
Param with name p3 has value: 'P3-Value'
Param with name p2 has value: 'P2-Value'
說明:
的表達document('')
選擇當前XSLT樣式表的文檔節點。
一個限制是當前的XSLT樣式表必須具有(可通過)URI訪問(例如駐留在給定文件中並且可以通過其文件名訪問) - 如果樣式表的上述代碼不會生成正確的結果是動態生成的(內存中的一個字符串)。
您引用的語言 - bash,ant - 是基於宏的語言,幾乎所有的事情都是在運行時發生的。 XSLT不是那樣的;它更像是基於編譯器的語言(C/Java等),其中變量和函數名稱純粹是編譯時構造。 –
@Michael,你不覺得這可能是這個問題的答案嗎?誰能比你更好地回答「爲什麼」? :) – Jarekczek