Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apostrophe text comparison in xsl

I have a problem with text with apostrophe symbol

example i try to test this xml having the symbol is then how can i compare ?

<xsl:for each select="country[nation='India's]">

this is statement showing error

Regards Nanda.A

like image 562
Nanda Avatar asked Mar 19 '10 16:03

Nanda


1 Answers

One way to do it would be:

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

<!-- ... later ... -->

<xsl:for-each select="country[nation=concat('India', $apos, 's')]">

The problem here is twofold:

  • XSLT defines no way of character escaping in strings. So 'India\'s' is not an option.
  • You must get through two distinct layers of evaluation.

These are:

  1. XML well-formedness: The XML document your XSLT program consists of must be well-formed. You cannot violate XML rules.
  2. XSLT expression parsing: The resulting attribute value string (after XML DOM parsing is done) must be make sense to the XSLT engine.

Constructs like:

<xsl:for-each select="country[nation='India's']">
<xsl:for-each select="country[nation='India&apos;s']">

pass the XML layer but violate the XSLT layer, because in both cases the effective attribute value (as stored in the DOM) is country[nation='India's'], which clearly is an XPath syntax error.

Constructs like:

<xsl:for-each select="country[nation=concat('India', "'", 's')]">
<xsl:for-each select="country[nation=concat("India", "&apos;", "s")]">

clearly violate the XML layer. But they would not violate the XSLT layer (!), since their actual value (if the XSLT document could be parsed in the first place) would come out as country[nation=concat('India', "'", 's')], which is perfectly legal as an XPath expression.

So you must find a way to pass through both layer 1 and layer 2. One way is the variable way as shown above. Another way is:

<xsl:for-each select="country[nation=concat('India', &quot;'&quot;, 's')]">

which would appear to XSLT as country[nation=concat('India', "'", 's')].

Personally, I find the "variable way" easier to work with.

like image 93
Tomalak Avatar answered Oct 31 '22 22:10

Tomalak