Jump to content

Any Replace(2.0) Function In 1.0


Krishn60
 Share

Recommended Posts

HiI have an XML file with an element at 3rd level having an attribute (a string of text). The text has multiple occurance of a special character & and I would like to replace all & with \T\. I am working in 1.0 version of XSLT. Can anyone please tell me if there is a function similar to replace in 1.0. If not, can you tell me how to do the replace by a sample example. I am trying to convert the string to a plain text file.ThanksKrishnan.

Link to comment
Share on other sites

There's EXSLT's str:replace() function, but AFAIK, it's not implemented natively anywhere. If you're using an XSLT processor that natively supports the func:function element, you can take the EXSLT function at the bottom of the str:replace() page, and import it into your XSLT. Otherwise, use the template implementation found on the same page.

Link to comment
Share on other sites

  • 5 months later...
Guest Chandra

Hi Krishnan,I am also facing the same problem, but replace function is not working with XSLT version 1.0 , i don't know whether it support or not?I know only use of template to replace the chars.Do u have any better idea and i how did u approached earlier?

Link to comment
Share on other sites

Hi Krishnan,I am also facing the same problem, but replace function is not working with XSLT version 1.0 , i don't know whether it support or not?I know only use of template to replace the chars.Do u have any better idea and i how did u approached earlier?
What XSLT processor are you using? Did you tried all of the options above? If only the template one worked, then you're out of luck - there's no better way in XSLT 1.0.
Link to comment
Share on other sites

There is a way with XSLT 1.0. By using recursion and string functions you can accomplish this. Note, my example uses CDATA to allow for the ampersand in the XML data

	<xml id="xmlSource">		<top>			<text><![CDATA[This&IsMy&Text]]></text>		</top>	</xml>

		<xsl:stylesheet			xmlns:xsl="http://www.w3.org/1999/XSL/Transform"			xmlns:msxsl="urn:schemas-microsoft-com:xslt"			version="1.0">			<xsl:output method="text" />			<xsl:template match="/">				<xsl:apply-templates select="//text"/>			</xsl:template>			<xsl:template match="text">				<xsl:call-template name="Replace">					<xsl:with-param name="value" select="text()"/>				</xsl:call-template>			</xsl:template>				<xsl:template name="Replace">				<xsl:param name="value" />		   		<xsl:variable name="StartString" select="substring-before($value,'&')" />		   		<xsl:variable name="EndString" select="substring-after($value,'&')" />				  <xsl:variable name="NewString">					  <xsl:value-of select="concat($StartString,'\T\',$EndString)"/>				</xsl:variable>					<xsl:choose>					<xsl:when test="contains($NewString,'&')">						<xsl:call-template name="Replace">							<xsl:with-param name="value" select="$NewString" />						</xsl:call-template>					</xsl:when>					<xsl:otherwise>						<xsl:value-of select="$NewString"/>					</xsl:otherwise>				</xsl:choose>			</xsl:template>		</xsl:stylesheet>

returnsThis\T\IsMy\T\Text

Edited by aalbetski
Link to comment
Share on other sites

You're still using a template, making it not much better than the one in EXSLT.On the other hand, yours doesn't rely on the exslt:node-set() function, as the EXSLT one appears to be.Just one thing... I'd also abstract away the "search" and "replacement" parts:

			<xsl:template name="Replace">				<xsl:param name="value" />				<xsl:param name="search" />				<xsl:param name="replacement" />				   <xsl:variable name="StartString" select="substring-before($value,$search)" />				   <xsl:variable name="EndString" select="substring-after($value,$search)" />				  <xsl:variable name="NewString">					  <xsl:value-of select="concat($StartString,$replacement,$EndString)"/>				</xsl:variable>					<xsl:choose>					<xsl:when test="contains($NewString,$replacement)">						<xsl:call-template name="Replace">							<xsl:with-param name="value" select="$NewString" />						</xsl:call-template>					</xsl:when>					<xsl:otherwise>						<xsl:value-of select="$NewString"/>					</xsl:otherwise>				</xsl:choose>			</xsl:template>

So that one can later do:

<xsl:call-template name="Replace">	<xsl:with-param name="value" select="text()"/>	<xsl:with-param name="search" select="'&'"/>	<xsl:with-param name="replacement" select="'\T\'"/></xsl:call-template>

or whatever kind of other search&replace is needed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...