Scott
Scott

Reputation: 131

XSL functions with xsl param as parameters

I will try to keep the terminology clear here, the overloading of terms is making this complicated.

I have a transform that I am passing an attribute to fill a xsl:param, this is working

<xsl:param name="platform"/>

This will print out if I use

<xsl:value-of select='$platform'/>

Instead of printing this attribute/param I need to pass it to a function. I have tried

<xsl:value-of select="replace(current(),'replaceMe','$platform')"/>

I receive the error

Invalid replacement string in replace(): $ sign must be followed by digit 0-9

Is there a way I can pass the param into the function?
If so, how do I need to format it?

Upvotes: 0

Views: 393

Answers (1)

Tomalak
Tomalak

Reputation: 338336

You mean

<xsl:value-of select="replace(current(), 'replaceMe', $platform)" />
  • $platform is a variable.

  • "$platform" is an invalid replacement string in the context of the replace() function, which works with regex and expects back-refereces like $1 or $2 in the replacement string when it sees $.


Logical conclusion: You will see the same error again as soon as the $platform variable accidentally contains a $, and another error when 'replaceMe' accidentally is an invalid regular expression. If it happens to be a valid regular expression and you don't know it, you might see other unexpected behavior.

Therefore - if you want to do verbatim search-and-replace with variables - you must properly regex-escape all special characters in the search pattern and at least escape any $ and \ in the replacement string. A robust "verbatim" replace call looks like this:

replace(
  $subject,
  replace($searchString, '[.\[\]\\|^$?*+{}()-]','\\$0'),
  replace($replacement, '[\\$]', '\\$0')
)

Upvotes: 2

Related Questions