Search code examples
xmlxsltreplaceescapingquotation-marks

XSLT replace single quotation sign with two single quotation signs


I'm writing XSLT code to parse data from an XML. Value of one element contains single quotation marks which I need to escape. I was trying to do something like this:

<xsl:variable name="text"><xsl:value-of select='desc_text'/></xsl:variable>
<xsl:value-of select="replace($text, '&apos;', '&apos;&apos;')"/>

but I get an error: xsl:value-of : could not compile select expression 'replace($text, ''', '''')'


Solution

  • Use <xsl:value-of select="replace($text, &quot;'&quot;, &quot;''&quot;)"/>, in XPath 2.0 and later you could also use (see https://www.w3.org/TR/xpath-31/#prod-xpath31-StringLiteral) <xsl:value-of select="replace($text, '''', '''''')"/>.