How to handle apostrophe and quote in XSLT

By xngo on February 21, 2019

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'_&quot;_a">apostrophe and quote: 123'_&quot;_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 &quot; to construct your string. Note: &quot; 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', &quot;'&quot;, '_a')]">
      <xsl:text>Found </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#xa;&#xa;</xsl:text> <!-- &#xa; means line feed, newline -->
    </xsl:for-each>
 
    <!-- Select textarea who's name is 123'_"_a -->
    <xsl:for-each select="//textarea[@name=concat('123', &quot;'&quot;, '_', '&quot;', '_a')]">
      <xsl:text>Found </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#xa;&#xa;</xsl:text>
    </xsl:for-each>
 
  </xsl:template>
 
</xsl:stylesheet>
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():___'___'__"___"__&#xa;</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.&#xa;&#xa;</xsl:text>
        </xsl:if>
 
    <!-- Replace double quotes with V. -->
    <xsl:value-of select='translate($aString, "&apos;", "A")'/>
 
        <!-- Display text if $aString contains ". -->
        <xsl:if test='contains($aString, "&apos;")'>
          <xsl:text>Found double quote(") with contains() function.&#xa;&#xa;</xsl:text>
        </xsl:if>
 
    <!-- Use &quot; instead of $quote variable. -->
    <xsl:value-of select="translate($aString, &quot;'&quot;, 'D')"/>
 
        <xsl:if test="contains($aString, &quot;'&quot;)">
          <xsl:text>Use &amp;quot; instead of $quote.&#xa;</xsl:text>
          <xsl:text>  Found double quote(') with contains() function.&#xa;&#xa;</xsl:text>
        </xsl:if>
 
  </xsl:template>
</xsl:stylesheet>
Translate():___V___V__"___"__
Found single quote(') with contains() function.
 
Translate():___A___A__"___"__
Found double quote(") with contains() function.
 
Translate():___D___D__"___"__
Use &quot; 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

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.