XML technologies / XSLT / XSLT Kochbuch / Making use of XSLT 2.0 / Processing string literals containing quotation marks
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
String literals containing quotation marks are difficult in XSLT 1.0 because it has no escape character.
Solution
An extension eases this problem by allowing a quotation mark to be escaped by doubling it. Here we match strings delimited by either double or single quotes. We use single quotes for the test attribute, so for the string literal regex we must use double quotes. Every literal double quote must therefore be escaped by doubling it in the regex. XML rules require us to use entity ' instead of ', but this merely emphasizes that XML escaping is a separate issue that does not solve the problem by itself. In other words, replacing " " with " " would satisfy the XML parser, but the XSLT parser would still fail:
<xsl:if test=' matches(., " "" [^""] "" | '[^'] ' ","x") '> </xsl:if>An equivalent solution is:
<xsl:if test=" matches(., ' &qout; [^"] " | ''[^''] '' ','x') "> </xsl:if>Discussion
The lack of an escape character in XSLT 1.0 was frustrating, but variables and concatenation always offered a workaround:
<xsl:variable name="d-quote" select='"'/>
<xsl:variable name="s-quote" select="'"/>
<xsl:value-of select="concat('He said,', $d-quote, 'John', $s-quote, 's', 'dog turned green.', $d-quote)"/>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de