Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt need to select a single quote

Tags:

html

xml

xslt

xpath

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;);'"/>
like image 208
Pacerier Avatar asked Jun 08 '11 08:06

Pacerier


2 Answers

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"
like image 118
Dimitre Novatchev Avatar answered Oct 13 '22 05:10

Dimitre Novatchev


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, ');')"/>
like image 30
Michael Kay Avatar answered Oct 13 '22 05:10

Michael Kay