Replace single quotes


/ Published in: XSLT
Save to your folder(s)

replacing single quotes with its (double-escaped) entity, for use of a text within xsl.


Copy this code and paste it in your HTML
  1. <xsl:template match="text">
  2. <xsl:variable name="apos">'</xsl:variable>
  3. <xsl:variable name="cleaned">
  4. <xsl:call-template name="_replace_string">
  5. <xsl:with-param name="string" select="." />
  6. <xsl:with-param name="find" select="$apos" />
  7. <xsl:with-param name="replace" select="'&amp;apos;'" />
  8. </xsl:call-template>
  9. </xsl:variable>
  10. <xsl:value-of select="$cleaned" />
  11. </xsl:template>
  12.  
  13. <xsl:template name="_replace_string">
  14. <xsl:param name="string" select="''"/>
  15. <xsl:param name="find" select="''"/>
  16. <xsl:param name="replace" select="''"/>
  17. <xsl:choose>
  18. <xsl:when test="contains($string,$find)">
  19. <xsl:value-of select="concat(substring-before($string,$find),$replace)"/>
  20. <xsl:call-template name="_replace_string">
  21. <xsl:with-param name="string" select="substring-after($string,$find)"/>
  22. <xsl:with-param name="find" select="$find"/>
  23. <xsl:with-param name="replace" select="$replace"/>
  24. </xsl:call-template>
  25. </xsl:when>
  26. <xsl:otherwise>
  27. <xsl:value-of select="$string"/>
  28. </xsl:otherwise>
  29. </xsl:choose>
  30. </xsl:template>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.