tags:

views:

37

answers:

1

Hi everybody, i have this funtion which tries to replace dots and/or - with _

I m limited to use xpath 1 so replace function is NOT an option. The template works not to much fine because if i use something like this:

FOO-BAR.THING-MADRID.html

it gives me out on screen this thing:

FOO-BAR.THING-MADRID.html

the middle dot is not replaced.

Someone could help me?

 <xsl:template name="replaceDots">
  <xsl:param name="outputString"/>
  <xsl:variable name="target">.</xsl:variable>
  <xsl:variable name="source">-</xsl:variable>
  <xsl:variable name="replacement">_</xsl:variable>
  <xsl:choose>
   <xsl:when test="contains($outputString,$source)">
    <xsl:value-of select="concat(substring-before($outputString,$source),$replacement)" disable-output-escaping="yes"/>
    <xsl:call-template name="replaceDots">
     <xsl:with-param name="outputString" select="substring-after($outputString,$source)"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:when test="contains($outputString,$target)">
    <xsl:value-of select="concat(substring-before($outputString,$target),$replacement)" disable-output-escaping="yes"/>
    <xsl:call-template name="replaceDots">
     <xsl:with-param name="outputString" select="substring-after($outputString,$target)"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$outputString" disable-output-escaping="yes"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
+1  A: 

To replace all dots or dashes with underscores, you do not need an <xsl:template>. You can use:

<xsl:value-of select="translate(., '-.', '__')" />

If you want to keep the ".html", you can extend this like so:

<xsl:value-of select="
  concat(
    translate(substring-before(., '.html'), '-.', '__'), 
    '.hmtl'
  )
" />

For a generic "string replace" template in XSLT, look at this question, for example.

Tomalak
@Tomalak: +1 Good answer.
Alejandro
ok, it works! thank you very much Tomalak.
@user: Good to hear! *If this completely solves your problem, please mark the answer as accepted. :-)*
Tomalak