converting speacial characters ?

Subject: converting speacial characters ?
From: Jukka.T.Lehtinen@xxxxxxxxx
Date: Wed, 13 Sep 2000 16:15:05 +0300
Hi All...

Well, I run to problems again. I need to check elements value that if it has
characters such as '\' or ':' or '"' and I need to replace them to :

\ -> \\
: -> \:
" -> \" (I'm using ")

So, I have source:

<test>
<para>sometext with : and \ and " and \  andi \\ or :</para>
</test>

and the result I want is:

<para>sometext with \: and \\ and \" and \\  andi \\\\ or \:</para>


OK. here is my code: 

It doesnt work, I thought that those when tests would chatch next (/":)
character, but... 

-------------------------

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


<xsl:template name="subststar">      
	<xsl:param name="from"/>

	<xsl:choose>
    	<xsl:when test="contains($from,'\') and string-length($from)!=0">
	     	<xsl:value-of
select="concat(substring-before($from,'\'),'\\')"/>	  
   	   	<xsl:call-template name="subststar">
    	       	<xsl:with-param name="from"
select="substring-after($from,'\')"/>
		</xsl:call-template>
	</xsl:when>

    	<xsl:when test="contains($from,':') and string-length($from)!=0">
	      <xsl:value-of
select="concat(substring-before($from,':'),'\:')"/>	  
   		<xsl:call-template name="subststar">
    	    	     <xsl:with-param name="from"
select="substring-after($from,':')"/>
		</xsl:call-template>
      </xsl:when>

    	<xsl:when test="contains($from,'$quot;') and
string-length($from)!=0">
	      <xsl:value-of
select="concat(substring-before($from,'$quot;'),'\$quot;')"/>	  
   		<xsl:call-template name="subststar">
    	            <xsl:with-param name="from"
select="substring-after($from,'$quot;')"/>
		</xsl:call-template>
	</xsl:when>

	    <xsl:otherwise>
			<xsl:value-of select="$from"/>
	    </xsl:otherwise>
	</xsl:choose>

</xsl:template>


<xsl:template match="test"> <!-- catches element and checks if we need
string replacement-->
	<testresult>
 		<xsl:if test="contains(., '&quot;') or contains(., ':') or
contains(., '\')">	
			<xsl:call-template name="subststar">
			 	<xsl:with-param name="from" select="."/>
			</xsl:call-template>
		</xsl:if>
	</testresult>
</xsl:template>

</xsl:stylesheet>
-------------------------------

result I get is: 

<testresult>
		sometext with : and \\ and " and \\  andi \\\\ or \:
</testresult>


So how should I do this? I know that this doesnt work because $from can be
used _kind of_ one time only.

Or is there any easier view to approach this problem??

p.s but I have made some progress in my XSLT's. Thanks to you !!

Jukka.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread