根據this SO問題應該可以使用XPath表達式中的參數match
。但是,如果在同一個模板中使用xsl:template
的xsl:param
,它似乎不起作用。在XSLT 2.0的匹配XPath表達式中不可能使用模板參數?
我的XML文件看起來如下
<?xml version="1.0" encoding="UTF-8"?>
<myRoot>
<myNode myAttribute="3">
<myChildAttribute myChildAttribute="a" />
</myNode>
<myNode myAttribute="2">
<myChildAttribute myChildAttribute="b" />
</myNode>
<myNode myAttribute="1" />
</myRoot>
和喜歡我的XSL文件。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="myRoot">
<xsl:apply-templates select="myNode">
<xsl:sort select="@myAttribute" />
<xsl:with-param name="myParam" select="max(myNode/@myAttribute)" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="myNode[node() and @myAttribute = $myParam]">
<xsl:param name="myParam" />
<xsl:for-each select="myChildAttribute">
INSERT INTO a(b) VALUES ('<xsl:value-of select="@myChildAttribute" />');
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
不幸的是,當與SAXON 9HE運行,出現以下錯誤
XPST0008: Variable myParam has not been declared (or its declaration is not in scope)
結束是沒可能使用模板的參數在相同模板匹配XPath表達式! ?
謝謝Dimitre!現在,我很清楚。 'xsl:sort'只是來自以前版本轉換的剩餘部分。感謝提示,我將刪除它。 – Jagger