|
Subject: RE: [xsl] 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 |
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, "I can''t"'">
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: [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="'"O'Reilly"'"/><!-- wrong -->
> <xsl:template match="/">
> <xsl:value-of select="$x"/>
> <xsl:text> </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 |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] String containing both si, David Carlisle | Thread | Re: [xsl] String containing both si, Michael Ludwig |
| Re: [xsl] String containing both si, David Carlisle | Date | Re: [xsl] String containing both si, Michael Ludwig |
| Month |