472,352 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

Replace in XSLT

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
5 21525
> 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
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
> 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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Geathaa | last post by:
Hi everyone, I want to transform a xml document containing the description of a menu tree to HTML. The MenuTree XML contains the target URL for...
3
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the...
5
by: Stephan Hoffmann | last post by:
Hi, I use XML mainly as a source for HTML. HTML browsers 'know' certain entity references like &eacute; or &auml;. When I use XSL to transform...
7
by: Yourself | last post by:
Hi, say I have the following XHTML document: <div id="Fred"> <div id="Bert"> <div id="Jim"> <p>Hello</p> <p>etc</p> </div> <div id="Bob">...
5
by: DarthDaddy | last post by:
I hope to explain this properly. Here is a sample section of a file I am working with: <achievements> <achievement> <item...
3
by: Alois Treindl | last post by:
A simple XSL question from a newbie: In an xml document which I transform via xsl into html output, I have some text which I want to be...
1
by: dewey | last post by:
Hi, This, I'm sure, is a very simple problem. I'm trying to use the xslt document() function to add a piece of information from a second xml...
1
by: Namshub | last post by:
I was wondering if there is a simple method of replacing characters within the xml document. I'm looking at writing a Biztalk Pipeline that...
2
by: rjoseph | last post by:
Hi Guys I hope this is a simple one for you. I am basically displaying data onto my xml page using the following line of code: <xsl:value-of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.