You can pass parameters to named templates that you call via <xsl:call-template>
, e.g.:
<xsl:call-template name="name">
<xsl:with-param name="param" select="xpathexpr"/>
</xsl:call-template>
<xsl:template name="name">
<xsl:param name="param"/>
...
</xsl:template>
When you call a named template, the context node is the current context. So to call a named template for child nodes, you need to change the current context by using <xsl:for-each>
:
<xsl:for-each select="child">
<xsl:call-template name="name">
<xsl:with-param name="param" select="xpathexpr"/>
</xsl:call-template>
</xsl:for-each>
In your case, though, there's no need to pass parameters, since the variable that you're trying to use is something that's navigable to from the context node. And you don't need to use all those variables (nor should you ever give a variable a name as useless as var1
):
<xsl:template match="folder">
<xsl:variable name="linkarg" value="concat(../../@path, '\', @name)"/>
<xsl:variable name="linktext" value="concat(../../@path, '', @name)"/>
<th colspan="2" align="left" bgcolor="#FF5500">
<a onclick="foo('{$linkarg}')">
<xsl:value-of select="$linktext"/>
</a>
</th>
</xsl:template>
Also, I'd be tempted to use ancestor::structure[1]/@path
rather than ../../@path
, because it makes the intention a lot more explicit; your version means "get the path
attribute from the parent of the parent element", while my version means "traverse up the chain of ancestor elements until you find the first one named structure
, and get its path
attribute."
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…