473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 21632
> 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 each tree node. Some URL's contain parameters...
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 following: Some text<br/> here with</br> lots...
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 XML to HTML or XML, these entities are replaced...
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"> <p>Goodbye</p>
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 name="COURSE_COMPLETION_DATE">19930630</item> <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 suppressed. The tags looks like this <anchor_ref...
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 file. The main xml has a list of football teams,...
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 escapes illegal characters before they are processed...
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 select="carmanufacturer" /> An example of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.