By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

Replace in XSLT

P: n/a
Hello,

it's very often neccessary to replace strings or a single character in
my stylesheets. My solution looks awful and very long. Now i thought
to solve this with an array like structure but i found no solution.
The original code looks like the following:

<!-- The replacement-function that i'm calling -->

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Every template which is calling this function must call it with three
params. The string which should be substituted, the string which
should be replaced and the replacement as follow:

<xsl:template match="option">
<xsl:variable name="content" select="."/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$content"/>
<xsl:with-param name="replace" select="'bla'"/>
<xsl:with-param name="with" select="'blub'"/>
</xsl:call-template>
</xsl:template>

This is a lot of awful code but much worse it is not possible to hand
over more then one replacement. Is there no better way. I thought
about a array-like structure to replace all the values in this array
at once. I'm stumped.

Has anyone a solution?
I would apprecciate any help.
Conny
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> it's very often neccessary to replace strings or a single character in
my stylesheets.This is a lot of awful code but much worse it is not possible to hand
over more then one replacement. Is there no better way? I thought
about a array-like structure to replace all the values in this array
at once. I'm stumped.

Hi,

You could take a look at http://www.topxml.com/xsltStylesheet..._recursion.asp . there's atemplate that will do ALL replacements.
I can't help you with designing a array algorithm, because I don't really understand how that should work. What is your idea?

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2

P: n/a
See also: "Two-stage recursive algorithms in XSLT", at:

http://www.topxml.com/xsl/articles/recurse/
Cheers,
Dimitre

"Joris Gillis" <ro**@pandora.be> wrote in message
news:op**************@news.pandora.be...
it's very often neccessary to replace strings or a single character in
my stylesheets.This is a lot of awful code but much worse it is not
possible to hand
over more then one replacement. Is there no better way? I thought
about a array-like structure to replace all the values in this array
at once. I'm stumped.

Hi,

You could take a look at
http://www.topxml.com/xsltStylesheet..._recursion.asp . there's
atemplate that will do ALL replacements.
I can't help you with designing a array algorithm, because I don't really
understand how that should work. What is your idea?

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum

Jul 20 '05 #3

P: n/a
> I can't help you with designing a array algorithm, because
I don't really understand how that should work. What is your idea?


Well I suppose i could solve the problem but the resulting code would
be quiet ugly and error-prone.
One way I thought of is to create a string with "tokens" inside. One
token is made of the string which should be replaced followed by a
minus followed by the replacement. The string contains as many tokens
as there must be replaced devided by a special character like a pipe.
It could look like:

replace-replacement|replace-replacement...

Now I call a template which is recursive with to params. The first
param is the string which contains the substrings that should be
replaced. The second param is the string with the replace-replacement
tokens. The template seperates the first token and assins the to be
replaced term to a variable and the replacement to a variable. Then it
calls the recursive template replace-string which i listed above with
the three params. After this it is calling themself.
I never tried it because it seems not be a good solution but it should
work.

Well I'm looking for a much better way with an array-like structure
but until now I have no idea :-(

regards
Conny
Jul 20 '05 #4

P: n/a
This is a lot of awful code but much worse it is not possible to hand
over more then one replacement. Is there no better way. I thought
about a array-like structure to replace all the values in this array
at once. I'm stumped.
xslt doesn't have arrays but it has node sets which are just what you
need here, you build an xml structure that encodes the pairs of lookup
string and replacement string and then iterate over that.

see for example the XSLT list FAQ entry here:

http://www.dpawson.co.uk/xsl/sect2/StringReplace.html

David

Jul 20 '05 #5

P: n/a
> I thought about a array-like structure to replace all the values in this array
at once.

I tried to device an array like technique in XSLT. It is available at
http://gandhimukul.tripod.com (no 1). Though I guess, that it may not
be directly useful for your problem.. I feel, true array like
operation is not possible in XSLT. The array operations, a[1] =
"value1" , a[2] = "value2" would require modifying the variable, which
XSLT does'nt allow.

Regards,
Mukul
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.