[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

converting speacial characters ?

Subject: converting speacial characters ?
From: Jukka.T.Lehtinen@xxxxxxxxx
Date: Wed, 13 Sep 2000 16:15:05 +0300
speacial characters
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.