I have faced an issue when using a variable as a condition for XPath evaluation. I have the following template which works fine:
<xsl:template name="typeReasonDic">
<xsl:variable name="dic" select="$schema//xs:simpleType[@name = 'type_reason_et']"/>
<!-- do something with the variable -->
</xsl:template>
However, when I change it to look like this:
<xsl:template name="typeReasonDic">
<xsl:param name="choose_dic" select="@name = 'type_reason_et'"/>
<xsl:variable name="dic" select="$schema//xs:simpleType[$choose_dic]"/>
<!-- do something with the variable -->
</xsl:template>
it fails to find the desired node.
What I wish to get is a template with a default value for $choose_dic which can be overriden where necessary.
What am I missing here?
UPD: there is this link I found with the description of what I’m trying to do, but it doesn’t seem to work for me.
You can’t do this directly in XSLT 1.0 or 2.0 without an extension function. The problem is that with
the
<xsl:param>will evaluate itsselectexpression a single time in the current context and store the true/false result of this evaluation in the$choose_dicvariable. The<xsl:variable>will therefore select either allxs:simpleTypeelements under the$schema(if$choose_dicis true) or none of them (if$choose_dic) is false. This is very different fromwhich will evaluate
@name = 'type_reason_et'repeatedly, in the context of eachxsl:simpleType, and select those elements for which the expression evaluated to true.If you store the XPath expression as a string you can use an extension function such as
dyn:evaluateor the XSLT 3.0xsl:evaluateelement if you’re using Saxon.