2010-09-16 48 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

相關問題