Re: Quoting quotes?

Subject: Re: Quoting quotes?
From: Mike Brown <mike@xxxxxxxx>
Date: Sat, 21 Oct 2000 19:19:09 -0600 (MDT)
Danny Vint wrote:
> <xsl:value-of select="substring-after('Dean. Well, let's see, we have on
> the bags, Who's on first, What's on second, I Don't Know is on third ... ',
> 'bags')"/>

I asked a similar question in August. Jeni Tennison's answer follows:

Date: Sun, 13 Aug 2000 10:04:17 +0100
To: Mike Brown <mike@xxxxxxxx>
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Subject: Re: how to put or test for quotes in a string
Cc: xsl-list@xxxxxxxxxxxxxxxx
In-Reply-To: <200008120642.AAA36692@xxxxxxxx>


Good question!  XML defines entities for ' and " (&apos; and &quot;,
somewhat unsurprisingly).  In certain situations, it is possible to use
these.  Your first example, for instance, could also be given as:

<xsl:variable name="foo" select="'&quot;Hello, world!&quot;'"/>

When this is parsed by the XML parser, the value of the 'select' attribute
is set to (no extra quotes included):

  '"Hello, world!"'

When the XSLT Processor sees this, it recognises the external quotes as
designating a string value, and so sets the variable $foo to the string (no
extra quotes included):

  "Hello, world!"

The thing to remember is that you are escaping the " and ' *for the XML
parser* and not for the XSLT processor.  So your second example:

>How do you test for the presence of ' or " in a string?
><!-- same $apos and $quot assignments, then... -->
><xsl:if test="contains($foo,$apos) or contains($foo,$quot)">
>  ...

can be escaped as:

<xsl:if test="contains($foo, &quot;'&quot;) or contains($foo '&quot;')">

As there are no unescaped "s within the attribute value, the XML parser can
parse this and emerges with the value of the 'test' attribute as:

  contains($foo, "'") or contains($foo, '"')

The XSLT processor can again recognise that "'" designates a string with
the value of the single character ' and that '"' designates a string with
the value of the single character ".

Similarly, if you wanted single quotes rather than double quotes around
your Hello, World!, then you should do:

<xsl:variable name="foo" select="&quot;'Hello, world!'&quot;" />

(-> "'Hello, world!'" att. value -> 'Hello, world!' string)

 [Or, alternatively:

<xsl:variable name="foo" select='"&apos;Hello, world!&apos;"' />

(-> "'Hello, world!'" att. value -> 'Hello, world!' string)]

So, for fairly simple situations like this, it is enough to use the normal
XML escaping to get the XSLT processor to see something that it can
understand.  However, difficulties arise when the quote nesting goes deeper
than this.  For example, if you wanted to see whether a string contains the
string (no extra quotes):

  "You're here"

There is no way to wrap quotes around that string, and no way that I know
of within XSLT/XPath to escape internal quotes like this (the XSLT
processor is not an XML parser - it won't detect and recognise
&quot;/&apos; itself).  In these cases, your method, using variables, is
the only solution.

(BTW, I'd personally declare the variables as:

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

so that they are set as strings rather than result tree fragments.)

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread
  • Quoting quotes?
    • Danny Vint - Sat, 21 Oct 2000 16:40:04 -0700
      • Mike Brown - Sat, 21 Oct 2000 19:19:09 -0600 (MDT) <=
      • <Possible follow-ups>
      • Kay Michael - Mon, 23 Oct 2000 09:29:38 +0100