[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] converting 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(., '"') 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|