tags:

views:

427

answers:

2

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!

+1  A: 

& 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; ..
Dewfy
+2  A: 

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.

Pavel Minaev
Unfortunately I forgot to mention that I'm limited to XPath 1.0 (I've just edited my question to mention it) but thanks for reminding me how simpler my life would be with XSLT 2.0/XPath 2.0 :)
Josh Davis
It would be truly great if decent XSLT 2.0 implementations started showing up, in particular for .NET - I run into these issues all the time too.
Eamon Nerbonne
Saxon.NET is quite good if you don't mind 25Mb of IKVM bloat in the distribution...
Pavel Minaev