xsl-list
[Top] [All Lists]

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

2008-11-28 06:49:52
In 2.0 you can escape the string delimiter by doubling it: ' as '', or " as
"". And of course you can escape the attribute delimiter using an XML entity
reference.

<xsl:if test="$x = 'He said, &quot;I can''t&quot'">

In 1.0 you can't have a string literal containing both single and double
quotes. Use concat:

<xsl:variable name="quot">"</xsl:variable>
<xsl:variable name="apos">'</xsl:variable>

<xsl:if test="$x = concat('He said, ', $quot, 'I can', $apos, 't', $quot)"> 

Michael Kay
http://www.saxonica.com/

-----Original Message-----
From: Michael Ludwig [mailto:mlu(_at_)as-guides(_dot_)com] 
Sent: 28 November 2008 11:43
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] String containing both single and double 
quotes (apos and quot) in XPath expression

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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--