[xsl] String containing both single and double quotes (apos and quot) in XPath expression

Subject: [xsl] String containing both single and double quotes (apos and quot) in XPath expression
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Fri, 28 Nov 2008 12:42:46 +0100
I want to hold a string containing both single and double quotes (apos
and quot) in a variable.

<xsl:variable name="x" select="'...'"/>

I enclose the XPath expression in double quotes, hence I'll have to use
entity references or numerical character references to refer to that
character from within the expression. Correct.

I enclose the string in single quotes, hence - I think - I'll have to
use entity references or numerical character references to refer to that
character from with the string. And this is wrong.

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="text"/>
  <!-- quot ist 34 (x22), apos ist 39 (x27) -->
  <xsl:variable name="x"
    select="'&quot;O&apos;Reilly&quot;'"/><!-- wrong -->
  <xsl:template match="/">
    <xsl:value-of select="$x"/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Trying to think about it a bit more logically, I find that XPath does
not require any of < > & to be treated specially, and that both entity
references and numerical character references don't have anything to do
with XPath - they're XML constructs.

So in this example, the XML parser resolves the references and the XPath
engine never gets to see them. Instead, it gets to see a syntax error.

Is this analysis correct?

It seems there is no way to include a single quote within a string that
is itself contained in single quotes. Conversely, the same applies for
double quotes. This is a bit hard to believe. Is it true?

I wish I could use a backslash! Have I missed anything?

I see two solutions in XSLT 1.0:

* I can write the xsl:variable as an RTF, which offers much better
  readability.

* I can split the string according to whether single or double quotes
  occur in it and use the XPath 1.0 concat() function to make it whole
  again.

Do I have more options?

In 2.0, instead of an RTF, I'd have a temporary tree. What else has
changed that is of immediate interest to the problem at hand?

Thanks.

Michael Ludwig

Current Thread