I’m trying to retrieve the attribute value from an xsl:param and use it in an xsl:if test condition.
So given the following xml
<product>
<title>The Maze / Jane Evans</title>
</product>
and the xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="test" select="Jane"/>
<xsl:template match="title[contains(., (REFERENCE THE SELECT ATTRIBUTE IN PARAM))]">
<h2>
<xsl:value-of select="substring-before(., '/')"/>
</h2>
<p>
<xsl:value-of select="substring-after(., '/')"/>
</p>
</xsl:template>
<xsl:template match="title">
<h2><xsl:value-of select="."/></h2>
</xsl:template>
</xsl:stylesheet>
I would like to get back
The Maze
Jane Evans
You have a problem in this line:
This defines an
xsl:paramnamedtest, whose value is the child element of the current node (‘/’) namedJane. As the top element is<product>and not<Jane>, thetestparameter has the value of an empty node-set (and a string value — the empty string).You want (notice the surrounding apostrophes):
The whole processing task can be implemented rather easily:
This XSLT 1.0 transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
The XSLT 1.0 syntax forbids variable/parameter references in a match pattern. This is why we have a single template matching any
titleand we specify inside the template the conditions for processing in a specific, wanted way.An XSLT 2.0 solution:
When this transformation is applied on the provided XML document (above), again the same wanted, correct result is produced:
Explanation:
XSLT 2.0 doesn’t have the limitations of XSLT 1.0 and variable/parameter references can be used within a match pattern.