0

I'm working at a java application that performs some xslt transformation. I would like to match nodes into the xslt document, using a parameter provided by java. Which is the right way to do something like:

<xsl:template match="//m:properties/*[contains($pattern,name())]"> 

because when I launch my application, it claims it's not able to compile the stylesheet, since pattern is not defined but I'm setting it using the setParameter method and I was able to use another parameter defined in the same way but in a different context. Thanks in advance Fil

3
  • Sorry, between ... and ... there was the code excerpt: <xsl:template match="//m:properties/*[contains($pattern,name())]"> Commented Oct 30, 2012 at 16:59
  • What version of XSLT (1.0 or 2.0)? Variable references are not allowed in match expressions in 1.0. Commented Oct 30, 2012 at 17:00
  • I'm afraid 1.0, is there any workaround? Commented Oct 30, 2012 at 17:00

1 Answer 1

1

You need an

<xsl:param name="pattern" />

in your stylesheet to declare the parameter, the setParameter call is not sufficient on its own. However there is a further problem that, according to the XSLT 1.0 spec, match expressions are not allowed to contain variable/parameter references like $pattern. Some processors do permit them anyway (including at least some versions of Xalan), but if it doesn't work then you need to change the matching logic, e.g. by defining the template to match //m:properties/* but then only calling apply-templates for those elements that match your pattern.

Sign up to request clarification or add additional context in comments.

2 Comments

Thaks Ian, declaring the parameter as you suggested, it worked :)
@filmac OK, but be careful if you're relying on the fact that the default processor violates the spec - TransformerFactory is pluggable, and if someone runs your app with a different (stricter) processor on the classpath it may fail to compile the stylesheet...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.