473,563 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="substri ng-before($text,$r eplace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substri ng-after($text,$re place)"/>
<xsl:with-param name="replace" select="$replac e"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<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="$conten t"/>
<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 21659
> it's very often neccessary to replace strings or a single character in
my stylesheets.Thi s 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.b e> wrote in message
news:op******** ******@news.pan dora.be...
it's very often neccessary to replace strings or a single character in
my stylesheets.Thi s 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|rep lace-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
4784
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 which are only known at runtime though. These "runtime parameters" are also set as global parameters in the XSLT stylesheet. What I want to do now is...
3
4758
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 of</br> inside it... Being <br/> HTML tags and not simple text like &lt;br/&gt;
5
2302
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 by what they refer to. Is there a way to avoid that?
7
1398
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
2234
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 name="COURSE_CODE">ADA1W0</item> <item name="SECTION_NUMBER">33</item> <item name="COURSE_DESC_CODE"/> <item name="COURSE_TYPE">DS</item>
3
5145
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 name="#B4">I. Introduction - page 4 </anchor_ref> <anchor_ref name="#B4">II. Childhood - page 24 </anchor_ref> <anchor_ref name="#B4">I. Later - page 42...
1
1600
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, wins, losses, goals scored etc. main xml:
1
4837
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 by a HL7 assembler. I.E. replace all instances of &amp; or & with \T\ . I thought about converting to a string and using the replace function, but...
2
6943
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 resulting output would be, "Ford"
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.