Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - "Embedded" XML in contains() and string-before() / string-after() functions. >Thread Next - Re: "Embedded" XML in contains() and string-before() / string-after() functions. Re: "Embedded" XML in contains() and string-before() / string-after() functions.To: NULL Date: 4/15/2008 1:23:00 PM riderchap@g... wrote: > I am trying to write an XSL transform to take an XML as input and > replace all single quotes ( ' ) with two single quotes ( '' ). The > situation is I am only sure about the name of the root tag of my XML. > Within the root tags I may have another tree of XML and I want to > replace all single quotes with two single quotes. > > My input XML will be like <TheRootTag>Some XML string</TheRootTag>. > > For an example that causing me problem > The input XML : > <TheRootTag><InnerTag ID="12'3"> <Tag1>This 's a test</Tag1></ > InnerTag></TheRootTag> > > Note that there are two single quote in the string above "12'3" and > "'s". Here is an XSLT 1.0 stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:attribute name="{name()}"> <xsl:call-template name="double-string"> <xsl:with-param name="text" select="."/> </xsl:call-template> </xsl:attribute> </xsl:template> <xsl:template match="text()"> <xsl:call-template name="double-string"> <xsl:with-param name="text" select="."/> </xsl:call-template> </xsl:template> <xsl:template match="comment() | processing-instruction()"> <xsl:copy/> </xsl:template> <xsl:template name="double-string"> <xsl:param name="text"/> <xsl:param name="string" select=""'""/> <xsl:choose> <xsl:when test="contains($text, $string)"> <xsl:value-of select="concat(substring-before($text, $string), $string, $string)"/> <xsl:call-template name="double-string"> <xsl:with-param name="text" select="substring-after($text, $string)"/> <xsl:with-param name="string" select="$string"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
