代码之家  ›  专栏  ›  技术社区  ›  aironman

有没有一种方法可以正确模拟XPath1上的replace函数?

  •  2
  • aironman  · 技术社区  · 14 年前

    我有一个函数,它试图将点和/或-替换为_

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

    福-酒吧。东西-马德里.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>
    
    1 回复  |  直到 11 年前
        1
  •  2
  •   Community CDub    7 年前

    要用下划线替换所有点或破折号,不需要 <xsl:template> . 您可以使用:

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

    如果你想保留 ".html"

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

    对于XSLT中的通用“字符串替换”模板, look at this question ,例如。