Josh Davis
Josh Davis

Reputation: 28730

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!

Upvotes: 2

Views: 2053

Answers (2)

Pavel Minaev
Pavel Minaev

Reputation: 101565

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.

Upvotes: 2

Dewfy
Dewfy

Reputation: 23624

& quot;& amp;&(!)apos; -looks much better, but what did you want to get?

In anyway: once I have written application that deals with producing of Javascript over XSLT. The same problem with huge number of & quot;,... we solved in 2 ways:

  1. Declare global xsl:param, $q - looks shorter than & quot;
  2. Use 'translate' XPath function, make assumption '!' - is a & quot;, # is a & amp; ..

Upvotes: 1

Related Questions