Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: "Embedded" XML in contains() and string-before() / string-after() functions.

From: Martin Honnen <mahotrash@-----.-->
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="&quot;'&quot;"/>
     <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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent