wolfmason
wolfmason

Reputation: 409

XSLT 1.0 - How to test an attribute contained in a parameter in an xsl:when statement

I have an XSLT template that passes a chunk of HTML into parameter. I need to pass that parameter to another template where it will be transformed one of two ways, depending upon the ID attribute of the top level HTML element contained in the parameter. Is it possible to test the content of a parameter with Xpath in XSLT 1.0? Here's my HTML:

<div id="someID">There could be many child nodes here</div>

Here's my XSLT test:

<xsl:when test="$content/@id = 'someID'">...</xsl:when>

Saxon 6.5.3 fails on this and tells me to switch to use exsl or specify version 1.1 (which has no impact), but I'd love to know if there's a clever way to achieve this using the tools I have in place.

Upvotes: 0

Views: 227

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117140

The parameter is declared in the template as follows: <xsl:param name="content"><xsl:apply-imports/></xsl:param>.

Strictly speaking, that's a variable, not a parameter. And its content is a result tree fragment. As the error message says, you must convert it to a node-set before you can process it further (in XSLT 1.0).

So do something like:

<xsl:variable name="content-rtf">
    <xsl:apply-imports/>
</xsl:variable>

<xsl:variable name="content" select="exsl:node-set($content-rtf)" />

<xsl:choose>
    <xsl:when test="$content/div/@id = 'someID'">
        <!-- some result -->
    </xsl:when>
    <xsl:otherwise>
        <!-- another result -->
    </xsl:otherwise>
</xsl:choose>

after adding:

xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"

to your opening <xsl:stylesheet> tag.


Untested because a reproducible example was not provided.

Upvotes: 1

user6622043
user6622043

Reputation: 101

<xsl:when test="$content/div[@id = 'someID']">...</xsl:when>

Upvotes: 0

Related Questions