2010-09-16 44 views
2

我有这个功能,它试图取代点和/或_有没有办法在XPATH 1上正确模拟替换函数?

我被限制使用xpath 1,所以替换功能不是一个选项。模板工程不以多少罚款,因为如果我用的是这样的:

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> 

回答

2

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

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

如果你想保持".html",可以延长这个像这样:

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

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

+0

+1好答案。 – 2010-09-16 14:29:17

+0

好的,它的工作原理!非常感谢Tomalak。 – aironman 2010-09-16 14:37:42

+0

@user:很高兴听到! *如果这完全解决了您的问题,请将答案标记为已接受。 : - )* – Tomalak 2010-09-16 14:52:33

相关问题