[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: String containing both single and double quotes (a

Subject: RE: String containing both single and double quotes (apos and quot) in XPath expression
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 28 Nov 2008 11:49:22 -0000
RE:  String containing both single and double quotes (a
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@xxxxxxxxxxxxx] 
> Sent: 28 November 2008 11:43
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.