Translate() Question

Gadrin77

Usenet Archive
Joined
Apr 6, 2004
Messages
22
Reaction score
0
I have data that looks like

<Root>
<Main Value="Line1|Line2.|Line3|Line4.|Line5"/>
</Root>

I'm using

Translate(@Value, "|.", ",")

to get rid of periods, and exchange the pipe character for a comma, which works fine.

what I really want to do is, once the above is done, change the comma to a comma and space ", " so it looks good in the output. I've tried Translate(Translate) but that didn't seem to work.

Any ideas?

I'm using MS XML 4 SP2 and XSL.
 

Ben Edgington

Usenet Archive
Joined
Mar 18, 2004
Messages
45
Reaction score
0
what I really want to do is, once the above is done, change the comma to a comma and space ", " so it looks good in the output. I've tried Translate(Translate) but that didn't seem to work.

As far as I can see translate can not lengthen strings, so you probably have to do full-blown search and replace which can be done with a recursive template, as follows. You may decide it's not worth the trouble 8^)

Your data

<Root>
<Main Value="Line1|Line2.|Line3|Line4.|Line5"/>
</Root>

with this transformation

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" omit-xml-declaration="yes"/>

<!-- The main template -->
<xsl:template match="/Root">

<xsl:call-template name="replace">
<xsl:with-param name="text" select="translate(Main/@Value, '.', '')"/> <xsl:with-param name="from" select="'|'"/> <xsl:with-param name="with" select="', '"/> </xsl:call-template>

</xsl:template>

<!-- This is a recursive named template for search and replace. --> <xsl:template name="replace"> <xsl:param name="text"/> <xsl:param name="from"/> <xsl:param name="with"/> <xsl:choose> <xsl:when test="$from and contains($text,$from)"> <xsl:value-of select="substring-before($text,$from)"/> <xsl:value-of select="$with"/> <xsl:call-template name="replace"> <xsl:with-param name="text" select="substring-after($text,$from)"/> <xsl:with-param name="from" select="$from"/> <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>

</xsl:stylesheet>

gives the desired output:

Line1, Line2, Line3, Line4, Line5

Ben
 

Gadrin77

Usenet Archive
Joined
Apr 6, 2004
Messages
22
Reaction score
0
Ben Edgington said:
<!-- This is a recursive named template for search and replace. --> <xsl:template name="replace"> <xsl:param name="text"/> <xsl:param name="from"/> <xsl:param name="with"/> <xsl:choose> <xsl:when test="$from and contains($text,$from)"> <xsl:value-of select="substring-before($text,$from)"/> <xsl:value-of select="$with"/> <xsl:call-template name="replace"> <xsl:with-param name="text" select="substring-after($text,$from)"/> <xsl:with-param name="from" select="$from"/> <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>

</xsl:stylesheet>

gives the desired output:

Line1, Line2, Line3, Line4, Line5

Ben

wow, thanks a lot Ben, that's nice. I've already put that to good use.

looks like I screwed up with removing the . using Translate since it removes all the periods in the data.

oh, well, I'm off to see if I can modify it to eliminate periods at the end of each token (Line) in the data.

thanks, mucho for your help, though.
 

Ben Edgington

Usenet Archive
Joined
Mar 18, 2004
Messages
45
Reaction score
0
looks like I screwed up with removing the . using Translate since it removes all the periods in the data.

oh, well, I'm off to see if I can modify it to eliminate periods at the end of each token (Line) in the data.

The search and replace might be handy here too. If you first replace '.|' with '|' before replacing '|' with ', ' it will do what you require. Use an xsl variable to store the intermediate result.

Obviously you have to be sure that none of your Line* tokens has a genuine '.' at the end.

Ben
 

Gadrin77

Usenet Archive
Joined
Apr 6, 2004
Messages
22
Reaction score
0
Ben Edgington said:
The search and replace might be handy here too. If you first replace '.|' with '|' before replacing '|' with ', ' it will do what you require. Use an xsl variable to store the intermediate result.

Obviously you have to be sure that none of your Line* tokens has a genuine '.' at the end.

Ben

yes, I haven't gotten that far yet (instead spent the afternoon learning how to group output), thanks for the advice. Hopefully I'll get to it on Monday.
 

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

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top