How to handle apostrophe and quote in XSLT

The XML sample

<?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>

The XSL codes:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Below are examples showing how to handle apostrophe and quote in XSLT
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="UTF-8" />
 
  <xsl:template match="*">
 
    <!-- Examples of how to handle apostrophe and quote in XPath -->
    <!-- ******************************************************* -->
    <!-- Searching for 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;</xsl:text><xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
 
    <!-- Searching for 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;</xsl:text><xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
 
    <!-- Examples of how to handle apostrophe and quote in XSL -->
    <!-- ***************************************************** -->
    <xsl:variable name="text">Translate():___'___'__"___"__&#xa;</xsl:variable>
 
    <!-- Embedded apostrophe in a variable and then use the variable -->
    <xsl:variable name="apos">'</xsl:variable>
    <xsl:value-of select="translate($text, $apos, 'V')"/>
 
    <xsl:if test="contains($text, $apos)">
      <xsl:text>Embedded apostrophe in a variable and then use it in contains()&#xa;&#xa;</xsl:text>
    </xsl:if>
 
    <!-- Use it between apostrophes -->
    <xsl:value-of select='translate($text, "&apos;", "A")'/>
 
    <xsl:if test='contains($text, "&apos;")'>
      <xsl:text>Use apostrophe between apostrophes: Used in contains()&#xa;&#xa;</xsl:text>
    </xsl:if>
 
    <!-- Use it between quotes -->
    <xsl:value-of select="translate($text, &quot;'&quot;, 'D')"/>
 
    <xsl:if test="contains($text, &quot;'&quot;)">
      <xsl:text>Use apostrophe between quotes: Used in contains()&#xa;&#xa;</xsl:text>
    </xsl:if>
  </xsl:template>
 
</xsl:stylesheet>

The output:

Found apostrophe: 123'_a
 
Found apostrophe and quote: 123'_"_a
 
Translate():___V___V__"___"__
Embedded apostrophe in a variable and then use it in contains()
 
Translate():___A___A__"___"__
Use apostrophe between apostrophes: Used in contains()
 
Translate():___D___D__"___"__
Use apostrophe between quotes: Used in contains()

References http://p2p.wrox.com/topic.asp?TOPIC_ID=52168 http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200312/msg00117.html http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes http://www.stylusstudio.com/xsllist/200104/post40540.html