有没有办法在 XPATH 1 上正确模拟替换功能? [英] is there a way to emulate correctly replace function on XPATH 1?

查看:24
本文介绍了有没有办法在 XPATH 1 上正确模拟替换功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数试图用 _ 替换点和/或 -

I have this function which tries to replace dots and/or - with _

我只能使用 xpath 1,所以替换功能不是一个选项.模板工作得不是很好,因为如果我使用这样的东西:

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

FOO-BAR.THING-MADRID.html

它让我在屏幕上看到这个东西:

it gives me out on screen this thing:

FOO-BAR.THING-MADRID.html

FOO-BAR.THING-MADRID.html

中间的点没有被替换.

有人可以帮助我吗?

 <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(., '-.', '__')" />

如果你想保留".html",你可以这样扩展:

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

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

例如,对于 XSLT 中的通用字符串替换"模板,看看这个问题.

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

这篇关于有没有办法在 XPATH 1 上正确模拟替换功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆