What's wrong with my <xsl:param>?
I have an xsl:param
that I'm trying to use to do a template match on an attribute. By everything I've found here and on the Internet, I'm doing this correctly. However, my output is blank.
Here is my xslt
<xsl:param name="strm_name">main</xsl:param>
<xsl:template match="stream[@name='{$strm_name}']"></xsl:template>开发者_运维百科
If I hardcode the param call to "main", this works just fine.
Here is the XML tag I'm trying to match to..<doc><stream name="main"></stream></doc>
Any help is much appreciated!
I see two issues:
- You cannot use a variable or parameter reference in a match pattern in XSLT 1.0
- You do not need the surrounding
'{...}'
when referencing your parameter in the predicate. (You're probably confusing this with an Attribute Value Template.) Use this instead:stream[@name=$strm_name]
A possible workaround for issue #1 is to select only those elements that meet the criteria controlled by your param. (You can reference a param in a select expression).
For example, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:param name="strm_name" select="'main'"/>
<xsl:template match="/">
<xsl:apply-templates select="/*/stream[@name=$strm_name]" />
</xsl:template>
<xsl:template match="stream">
<xsl:apply-templates />
<xsl:text>/</xsl:text>
</xsl:template>
</xsl:stylesheet>
Applied to this document:
<root>
<stream name="main">1</stream>
<stream name="other">2</stream>
<stream name="main">3</stream>
<stream name="main">4</stream>
<stream name="other">5</stream>
<stream name="other">6</stream>
</root>
...matches only the desired nodes. Output:
1/3/4/
精彩评论