xsl-list
[Top] [All Lists]

Re: XSLT: text()='''

2003-07-11 07:20:34
Hi Chris,

As part of my stylesheet I was replacing a few terms using the
following construct, but I appear to be having a problem with this
particular string replacement as it contains an ' entity. Can
anyone suggest what I'm doing wrong please?

XSLT:
  <xsl:template match="//subjectterm[text()='Employees&apos; rights and
obligations']">
    <subjectterm>Employees rights</subjectterm>
  </xsl:template>

The &apos; and &quot; escapes are XML escapes that you have to use
within attribute values that are delimited by ' and " respectively. In
other words, if you have an XML attribute like foo="..." then any "
within the value has to be escaped with &quot; and if you have an XML
attribute like foo='...' then any ' within the value has to be escaped
with &apos;.

You don't need to use &apos; in an attribute value delimited by ". In
your example above, you can use ' or &apos; and the effect will be the
same.

When the attribute value is reported to an application (such as an
XSLT processor) by an XML parser, the &apos; and &quot; escapes are
converted into the relevant characters. In the above example, the
attribute value reported to the XSLT processor is:

  //subjectterm[text()='Employees' rights and obligations']

This is an XPath, but it's not a legal XPath because the ' within the
string literal is being interpreted as closing the string literal. The
XSLT processor is reporting an error because it can't parse the
attribute value.

To make the XPath legal, it has to look like:

  //subjectterm[text()="Employees' rights and obligations"]

In other words, the string literal has to be delimited by "s so that
the ' within the string isn't interpreted as the end of the string
literal.

If you put that into an XML document as an attribute value, then you
have to escape either the ' or the " depending on what delimiter you
use for the attribute. So you should use either:

  match="//subjectterm[text()=&quot;Employees' rights and obligations&quot;]"

or:

  match='//subjectterm[text()="Employees&apos; rights and obligations"]'

(In XPath 2.0, you can use '' in a string literal delimited by 's to
indicate a ' within the string literal, and similarly you can use ""
within a string literal delimited by "s to indicate a ".)

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>