7

i need to do this:

<xsl:with-param name="callme" select="'validateInput(this,'an');'"/>

I've read Escape single quote in xslt concat function and it tells me to replace ' with &apos; I've done that yet its still not working..

Does anyone know how do we fix this?:

<xsl:with-param name="callme" select="'validateInput(this,&apos;an&apos;);'"/>
Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634

4 Answers4

9

Something simple that can be used in any version of XSLT:

<xsl:variable name="vApos">'</xsl:variable>

I am frequently using the same technique for specifying a quote:

<xsl:variable name="vQ">"</xsl:variable>

Then you can intersperse any of these variables into any text using the standard XPath function concat():

concat('This movie is named ', $vQ, 'Father', $vApos, 's secrets', $vQ)

So, this transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

  <xsl:variable name="vApos">'</xsl:variable>
  <xsl:variable name="vQ">"</xsl:variable>

    <xsl:template match="/">
        <xsl:value-of select=
        "concat('This movie is named ', $vQ, 'Father', $vApos, 's secrets', $vQ)
    "/>
    </xsl:template>
</xsl:stylesheet>

produces:

This movie is named "Father's secrets"
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
4

In XSLT 2.0 you can the character used as a string delimiter by doubling it, so

<xsl:with-param name="callme" select="'validateInput(this,''an'');'"/>

Another solution is to use variables:

<xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="quot">"</xsl:variable>

<xsl:with-param name="callme" select="concat('validateInput(this,', $apos, 'an', $apos, ');')"/>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
3

This is a little tricky, but you need to invert the apostrophe and quotes, like this:

<xsl:with-param name="callme" select='"validateInput(this,&apos;an&apos;);"' />

You're enclosing a string within one set of quotes, and the attribute value that contains it in another. In XSLT, which quotes you use are interchangeable in both cases, as long as you don't use the same one.

Previously, your &apos; was being parsed as the value of the match attribute was being read, and it was trying to set the value of the select to 'validateInput(this,'an');'. Although this is technically a valid string value, when XSLT processes it, it fails to parse it because it's tries to read it as a string literal, which is terminated prematurely before the an, as the same apostrophe is used there as was used to enclose the string.

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
1

Use &quot; rather than &apos; (by using &apos; you are effectively nesting single quotation marks inside single quotation marks; you need to alternate single and double quotation marks as you nest, escaping as necessary).

alexbrn
  • 2,050
  • 13
  • 11
  • yes - and that is what is required at this point (it makes no difference to the XPath expression normally). If for some peculiar reason is is really necessary to have single quotation marks in the string, you need to alternate the single/double differently as so: – alexbrn Jun 08 '11 at 08:41
  • But my point is, that ' is part of the value that's being provided; changing it to " changes the value being passed; that's very different. It's obviously program code, and in some language, strings are enclosed in `'`, not `"`, such as Delphi. – Flynn1179 Jun 08 '11 at 08:43
  • As I understand, URL needs single quote and not double. So concate in xsl must be in single quotes too. It is sort of crazy... :-( – xerostomus Nov 21 '22 at 20:54