Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: how to find deep nested double quotes in a string using XSLT

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 8/1/2005 5:46:00 PM

Satya Karri wrote:


> I have a string  -  Hello"world",test "purpose"
> 
> I want to use XLST to turn this one into - "Hello""world"",test ""purpose"""
> 
> I try to replace every double quote into two doublequotes for the purpose of 
> outputting CSV file format using XSLT.

You need to write a recursive template that looks for a double quote, 
takes the string before it, adds two double quote characters, and calls 
the template on the remaining string.

So if you want to transform
   Hello"world",test "purpose"
into
   Hello""world"",test ""purpose""
you could do it alike this:

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

<xsl:param name="input"
select="'Hello&quot;world&quot;,test &quot;purpose&quot;'" />
<xsl:param name="defaultCharacter" select="'&quot;'" />

<xsl:template match="/">
   <xsl:call-template name="duplicateCharacter">
     <xsl:with-param name="inputString" select="$input" />
   </xsl:call-template>
</xsl:template>

<xsl:template name="duplicateCharacter">
   <xsl:param name="inputString" />
   <xsl:param name="characterToDuplicate" select="$defaultCharacter" />
   <xsl:choose>
     <xsl:when test="contains($inputString, $characterToDuplicate)">
       <xsl:variable name="head"
                     select="concat(substring-before($inputString, 
$characterToDuplicate), $characterToDuplicate, $characterToDuplicate)" />
       <xsl:variable name="tail">
         <xsl:call-template name="duplicateCharacter">
           <xsl:with-param name="inputString" 
select="substring-after($inputString, $characterToDuplicate)" />
           <xsl:with-param name="characterToDuplicate" 
select="$characterToDuplicate" />
         </xsl:call-template>
       </xsl:variable>
       <xsl:value-of select="concat($head, $tail)" />
     </xsl:when>
     <xsl:otherwise><xsl:value-of select="$inputString" /></xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>


Or look into existing solutions to replace a substring with another 
string, one is here:
   <http://www.exslt.org/str/functions/replace/index.html>


-- 

	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