Replace in XSLT

K

K.Simon

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:eek:therwise>
<xsl:value-of select="$text"/>
</xsl:eek:therwise>
</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
 
J

Joris Gillis

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/xsltStylesheets/xslt_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,
 
K

K.Simon

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
 
D

David Carlisle

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
 
M

Mukul Gandhi

I thought about a array-like structure to replace all the values in this array
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top