Translate() Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gadrin77

    #1

    Translate() Question

    I have data that looks like


    <Root>
    <Main Value="Line1|Li ne2.|Line3|Line 4.|Line5"/>
    </Root>

    I'm using

    Translate(@Valu e, "|.", ",")

    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(Trans late()) but that didn't seem to work.

    Any ideas?

    I'm using MS XML 4 SP2 and XSL.
  • Ben Edgington

    #2
    Re: Translate() Question

    gadrin7@aol.com (Gadrin77) writes:[color=blue]
    > 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(Trans late()) but that didn't seem to work.[/color]

    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|Li ne2.|Line3|Line 4.|Line5"/>
    </Root>


    with this transformation


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

    <xsl:output method="xml" omit-xml-declaration="ye s"/>

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

    <xsl:call-template name="replace">
    <xsl:with-param name="text" select="transla te(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="substri ng-before($text,$f rom)"/>
    <xsl:value-of select="$with"/>
    <xsl:call-template name="replace">
    <xsl:with-param name="text" select="substri ng-after($text,$fr om)"/>
    <xsl:with-param name="from" select="$from"/>
    <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>

    </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.

    Comment

    • Gadrin77

      #3
      Re: Translate() Question

      Ben Edgington <usenet@edginet .org> wrote in message news:<87pt9ft6z o.fsf@edginet.o rg>...
      [color=blue]
      > <!-- 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="substri ng-before($text,$f rom)"/>
      > <xsl:value-of select="$with"/>
      > <xsl:call-template name="replace">
      > <xsl:with-param name="text" select="substri ng-after($text,$fr om)"/>
      > <xsl:with-param name="from" select="$from"/>
      > <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>
      >
      > </xsl:stylesheet>
      >
      >
      > gives the desired output:
      >
      >
      > Line1, Line2, Line3, Line4, Line5
      >
      >
      > Ben[/color]

      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.

      Comment

      • Ben Edgington

        #4
        Re: Translate() Question

        gadrin7@aol.com (Gadrin77) writes:[color=blue]
        > 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.[/color]

        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.

        Comment

        • Gadrin77

          #5
          Re: Translate() Question

          Ben Edgington <usenet@edginet .org> wrote in message news:<87pt9d7n4 0.fsf@edginet.o rg>...[color=blue]
          > gadrin7@aol.com (Gadrin77) writes:[color=green]
          > > 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.[/color]
          >
          > 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[/color]


          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.

          Comment

          Working...