473,388 Members | 1,340 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,388 software developers and data experts.

Translate() Question

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.
Jul 20 '05 #1
4 2129
ga*****@aol.com (Gadrin77) writes:
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:output 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:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
gives the desired output:
Line1, Line2, Line3, Line4, Line5
Ben

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #2
Ben Edgington <us****@edginet.org> wrote in message news:<87************@edginet.org>...
<!-- 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:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</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.
Jul 20 '05 #3
ga*****@aol.com (Gadrin77) writes:
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

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #4
Ben Edgington <us****@edginet.org> wrote in message news:<87************@edginet.org>...
ga*****@aol.com (Gadrin77) writes:
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

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.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bengt Richter | last post by:
Just thought None as the first argument would be both handy and mnemonic, signifying no translation, but allowing easy expression of deleting characters, e.g., s = s.translate(None,...
6
by: calfliu | last post by:
hello: everybody! I have a simple question. How can I translate the selected value from art to recipient(hidden input button)? <form name="send" method="post" action="send.cgi"> <select...
5
by: Marcos | last post by:
I need to translate the messages of mine scripts, as I can make professionally this working with the pair php and javascript? Thank`s -- Marcos. http://www.linuxhard.org
0
by: SimpleSimple | last post by:
My company has an IIS 5, ASP.net intranet site that contains documents for employee use. The files are of various types (most often Office) and are stored in blob fields in a SQL2000 database. ...
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
1
by: peterbe | last post by:
This has always worked fine for me. Peter fine Now if I do it with a unicode string: Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/string.py", line...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
3
by: Kenneth McDonald | last post by:
I have the need to occasionally translate a single word programatically. Would anyone have a Python script that would let me do this using Google (or another) translation service? Thanks, Ken
4
by: kovariadam | last post by:
Hi, Does anybody know why i get this error: SQL0176N The second, third or fourth argument of the TRANSLATE scalar function is incorrect. SQLSTATE=42815 with this query: SELECT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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...

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.