开发者

is there a way to emulate correctly replace function on XPATH 1?

I have this function 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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜