J. Lev
J. Lev

Reputation: 317

How to compare two string that contains single quote( ' ) in xsl

I have to compare two strings containing single quote in xsl-1.0, but when I test them, it never works. I've tried with

<xsl:if test="/name= 'start of string d&apos; end of string'">
<xsl:if test="/name= 'start of string ' end of string'">

But in those situations I get an error telling me that end of string was unexpected

and by changing the quotes by single quotes

    <xsl:if test='/name= "start of string ' end of string"'>

It build but the test return false even though it clearly should return true.

What am I doing wrong ?

Upvotes: 1

Views: 1270

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

Use XML escaping for the character being used as an XML attribute delimiter, and XPath escaping for the character being used as an XPath string delimiter.

So if the attribute delimiter is ", it can be escaped as &quot; while if it is ', it can be escaped as &apos;

XPath escaping exists only in XPath 2.0. If the string delimiter is ", it can be escaped as "", while if it is ', it can be escaped as ''.

In XPath 1.0 that leaves you with a problem. The way to solve it is by binding variables either to the quotation marks

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

or to the actual string literals

<xsl:variable name="s">He said "Please don't jump"</xsl:variable>

and then using the variables in XPath expressions in place of string literals.

Upvotes: 3

Related Questions