This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: replace function


"Stevenson Ngila" <Stevenson at epr dot footman-walker dot com> wrote:

> All what i want to achieve is get a string i.e. "steve,john,peter"
and
> replace the next word to a comma  with a space hence the new string 
> become "steve, john, peter" how can i achieve this using the 
> translate function?

The translate() function can be used to perform only 1:1 or 1:0
character replacement. What you want here is 1 : 2.

Using the "str-map" template from FXSL, this is done in a most
straightforward manner:

stylesheet:
----------
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:testmap="f:testmap"
>
   <xsl:import href="str-map.xsl"/>
   
   <xsl:output method="text"/>
   
   <!-- to be applied on any xml source -->
   
   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>
   
   <xsl:template match="/">
     <xsl:variable name="vTestMap" 
                   select="document('')/*/testmap:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vTestMap"/>
       <xsl:with-param name="pStr" select="'steve,john,peter'"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="replAmpersand" match="testmap:*">
      <xsl:param name="arg1"/>
      
      <xsl:copy-of select="$arg1"/>
      <xsl:if test="$arg1 = ','">
        <xsl:value-of select="' '"/>
      </xsl:if>

    </xsl:template>

</xsl:stylesheet>


When applied on any source xml document, the result is:

steve, john, peter


Hope this helped.




=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]