Handle quotes in XPath
Suppose that you have the following XML input file.
<?xml version="1.0" encoding="UTF-8"?> <root> <textarea name="123'_a">apostrophe: 123'_a</textarea> <textarea name="123">b</textarea> <textarea name="123">a</textarea> <textarea name="123'_"_a">apostrophe and quote: 123'_"_a</textarea> <textarea name="123345">c</textarea> <textarea name="555">a</textarea> </root>
To escape single and double quotes in XPath, you have to use concat()
function and "
to construct your search string.
Note: "
means double quotes.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" encoding="UTF-8" /> <xsl:template match="*"> <!-- Select textarea who's name is 123'_a --> <xsl:for-each select="//textarea[@name=concat('123', "'", '_a')]"> <xsl:text>Found </xsl:text> <xsl:value-of select="."/> <xsl:text>

</xsl:text> <!-- 
 means line feed, i.e. newline --> </xsl:for-each> <!-- Select textarea who's name is 123'_"_a --> <xsl:for-each select="//textarea[@name=concat('123', "'", '_', '"', '_a')]"> <xsl:text>Found </xsl:text> <xsl:value-of select="."/> <xsl:text>

</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The output:
Found apostrophe: 123'_a Found apostrophe and quote: 123'_"_a
Handle quotes in XSL
In XSL, the easiest way to handle quote is to assign it to a variable. And then, use that variable instead of the quote character. Here are some examples on how to manipulate the string Translate():___'___'__"___"__.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" encoding="UTF-8" /> <xsl:template match="*"> <!-- Sample string --> <xsl:variable name="aString">Translate():___'___'__"___"__
</xsl:variable> <!-- Assign ' to the $quote variable. --> <xsl:variable name="quote">'</xsl:variable> <!-- Replace single quote with V. --> <xsl:value-of select="translate($aString, $quote, 'V')"/> <!-- Display text if $aString contains '. --> <xsl:if test="contains($aString, $quote)"> <xsl:text>Found single quote(') with contains() function.

</xsl:text> </xsl:if> <!-- Replace double quotes with V. --> <xsl:value-of select='translate($aString, "'", "A")'/> <!-- Display text if $aString contains ". --> <xsl:if test='contains($aString, "'")'> <xsl:text>Found double quote(") with contains() function.

</xsl:text> </xsl:if> <!-- Use " instead of $quote variable. --> <xsl:value-of select="translate($aString, "'", 'D')"/> <xsl:if test="contains($aString, "'")"> <xsl:text>Use &quot; instead of $quote.
</xsl:text> <xsl:text> Found double quote(') with contains() function.

</xsl:text> </xsl:if> </xsl:template> </xsl:stylesheet>
The output:
Translate():___V___V__"___"__ Found single quote(') with contains() function. Translate():___A___A__"___"__ Found double quote(") with contains() function. Translate():___D___D__"___"__ Use " instead of $quote. Found double quote(') with contains() function.
References
- https://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200312/msg00117.html
Github
- https://github.com/xuanngo2001/xslt-examples/blob/master/quote/input.xml
- https://github.com/xuanngo2001/xslt-examples/blob/master/quote/quotes-xpath.xsl
- https://github.com/xuanngo2001/xslt-examples/blob/master/quote/quotes-xsl.xsl