Search code examples
xsltxpathxpath-1.0

How to express a string containing both apostrophes and quotes in XPath?


I'll spare you the details because they would be needlessly confusing. Long story short, I'm using XSLT 1.0 to generate XSL documents, I'm trying to compare a variable to a literal string, and that string may contain quotes and apostrophes.

For the sake of simplicity, let's say that this literal is composed of two characters: a quote followed by an apostrophe. In reality, it can be any text really. Is there a simpler way to do this:

<xsl:if test="$var = concat('&quot;', &quot;'&quot;)">

than this?

<xsl:variable name="str">"'</xsl:variable>
<xsl:if test="$var = $str">

I have checked XPath's specs and there doesn't seem to be a way to escape characters, so the following would not work as desired:

<xsl:if test="$var = '&quot;&amp;apos;'">

Thanks!


Solution

  • There's no way to do it neatly in XPath 1.0. In XPath 2.0, you can escape both kinds of quotes by doubling.