XSLT – munging apostrophes

Escaping escapes can be a near impossible task with XSLT.

Here is the standard form of testing whether a string matches


<xsl:if
test=”countryName=’USA’” >
Name matches
</xsl:if>

What happens if the country name is People’s Republic of China instead?

  1. You can’t use the apostrophe because the single quote is already used to delimit the string.
  2. You can’t use double quotes around the string because it is already used to delimit the attribute
  3. You can’t use the &apos; entity because it is converted to a single quote inside the attribute, so the attribute sees 'People's Republic of China' any way (which is an illegal expression).

Solution 1 – use string concatenation


<xsl:if
test=”countryName=concat(‘People,’, &quot;&apos;&quot;, ’s Republic of China’)” >
Name matches
</xsl:if>

Solution 2 – use a variable


<xsl:variable name=”apos”>'</xsl:variable>
<xsl:if
test=”countryName=concat(‘People,’, $apos, ’s Republic of China’)” >
Name matches
</xsl:if>

Solution 3 – thanks Jörn Horstmann


<xsl:variable name=”PRC”>People's Republic of China</xsl:variable>
<xsl:if
test=”countryName=$PRC” >
Name matches
</xsl:if>
Bookmark and Share

You should follow me on twitter here

7 Responses to “XSLT – munging apostrophes”

  1. ^love*encounter~flow writes:

    it is generally a bad idea to use a language (xslt here) which demands that you regularly put the very expressions that carry your programmatic intent into ‘escrow’ so to speak. i think it is bad language design. in this case, it is an outflow of the abuse of xml to express procedural content. look, the problems do not start at the apostrophe. they start when you have to put ‘if’ into pointy brackets. xml is so ‘stupid’, it can’t parse the word ‘if’, so to speak. i’ve seen shell scripts that call php that contains sql and so on, and i always tell people don’t but people won’t listen. using plain xml can be a pretty bad idea, but using xml to express a program that processes xml is worse. you want to have a decent, readable declarative language or a decent, readable procedural language for that purpose. xml is neither.

  2. Jörn Horstmann writes:

    How about using a variable to hold the string to compare, like this

    People’s Republic of China

    Name matches

  3. Chui writes:

    @love-encounter-flow XPath expressions inside XSLT is one of the warts in XSLT. Otherwise, we are so deeply invested already there was no choice but to workaround the problems. By the way, one saving grace of XSLT is that it’s actually a functional language, with no mutation allowed.

    @Jörn yes that would work. I think WP swallowed your XML tags. I’ve included your solution in the post.

  4. Marius Gedminas writes:

    Can’t you use

    test=”countryName="People’s Republic of China"”

    or

    test=’countryName=”People's Republic of China”‘

    ?

    BTW would you please consider providing an RSS feed with full articles on Planet Python?

  5. Marius Gedminas writes:

    Yikes, the comment form interpreted my examples as HTML markup. Talk about metal-level confusion. I meant (and, since your blog has no ‘Preview’ button, can only hope that this’ll work):

    test=”countryName=&quot;People’s Republic of China&quot;”

    and

    test=’countryName=”People&apos;s Republic of China”‘

  6. Jarno Elovirta writes:

    Solution 4 – use apostrophes for attribute value

  7. Jarno Elovirta writes:

    (WP ate the example)

Leave a Reply