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?
- You can’t use the apostrophe because the single quote is already used to delimit the string.
- You can’t use double quotes around the string because it is already used to delimit the attribute
- You can’t use the ' 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,’, "'", ‘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>
About this entry
You’re currently reading “ XSLT – munging apostrophes ,” an entry on Chui's Counterpoint
- Published:
- 7.14.09 / 5pm
- Category:
- General
7 Comments
Jump to comment form | comments rss [?] | trackback uri [?]