Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl: how to use parameter inside "match"?

Tags:

xslt

My xsl has a parameter

<xsl:param name="halfPath" select="'halfPath'"/>

I want to use it inside match

<xsl:template match="Element[@at1='value1' and not(@at2='{$halfPath}/another/half/of/the/path')]"/>

But this doesn't work. I guess a can not use parameters inside ''. How to fix/workaround that?

like image 375
Oleg Vazhnev Avatar asked May 24 '11 22:05

Oleg Vazhnev


2 Answers

The XSLT 1.0 W3C Specification forbids referencing variables/parameters inside a match pattern.:

"It is an error for the value of the match attribute to contain a VariableReference"

There is no such limitation in XSLT 2.0, so use XSLT 2.0.

If due to unsurmountable reasons using XSLT2.0 isn't possible, put the complete body of the <xsl:template> instruction inside an <xsl:if> where the test in conjunction with the match pattern is equivalent to the XSLT 2.0 match pattern that contains the variable/parameter reference(s).

In a more complicated case where you have more than one template matching the same kind of node but with different predicates that reference variables/parameters, then a wrapping <xsl:choose> will need to be used instead of a wrapping <xsl:if>.

like image 97
Dimitre Novatchev Avatar answered Oct 01 '22 23:10

Dimitre Novatchev


Well, you could use a conditional instruction inside the template:

<xsl:template match="Element[@at1='value1']">
  <xsl:if test="not(@at2=concat($halfPath,'/another/half/of/the/path'))">
    .. do something
  </xsl:if>
</xsl:template>

You just need to be aware that this template will handle all elements that satisfy the first condition. If you have a different template that handles elements that match the first, but not the second, then use an <xsl:choose>, and put the other template's body in the <xsl:otherwise> block.

Or, XSLT2 can handle it as is if you can switch to an XSLT2 processor.

like image 27
Flynn1179 Avatar answered Oct 02 '22 00:10

Flynn1179