我想声明一个变量并根据属性值和参数值选择节点。
示例:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<group>
<item show='false'>item1</item>
<item show='true'>item2</item>
<item show='false'>item3</item>
</group>
<group>
<item show='false'>item4</item>
<item show='true'>item5</item>
</group>
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:call-template name="atemplate">
<xsl:with-param name="aparam" select="true"/>
</xsl:call-template>
<br/>
<xsl:call-template name="atemplate"/>
</body>
</html>
</xsl:template>
<xsl:template name="atemplate">
<xsl:param name="aparam" select="'false'"/>
<xsl:variable
name="items"
select=".//item[$aparam = 'false' or ($aparam = 'true' and @show = 'true')]"
/>
<table>
<xsl:for-each select="$items">
<tr>
<td><xsl:value-of select="text()"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
结果:
项目1
项目2
项目3
项目4
第5项
想要的结果:
项目2
第5项
项目1
项目2
项目3
项目4
第5项
目前它按预期工作,谢谢@MartinHonnen 的评论:
<xsl:call-template name="atemplate">
<!-- The problem was in that the parameter passed as select="true"-->
<xsl:with-param name="aparam" select="'true'"/>
</xsl:call-template>