2009-06-16 101 views
0

我無法編輯XML,我只想更改XSLT文件中的XML數據。替換XSLT中的XML值

<xsl:value-of select="Name" disable-output-escaping="yes"/> 

XML數據的價值是"Northfield Bancorp Inc.(MHC)",我想用"Northfield Bancorp Inc."來取代它(刪除"MHC")。

XSLT中是否有可用的搜索和替換函數?

回答

5

如果它僅僅是「(MHC)」你要刪除的字符串的結尾,這將做到:

<xsl:value-of select=" 
    substring-before(
    concat(Name, '(MHC)'), 
    '(MHC)' 
) 
" /> 

如果你想動態替換,你可以寫這樣的功能:

<xsl:template name="string-replace"> 
    <xsl:param name="subject"  select="''" /> 
    <xsl:param name="search"  select="''" /> 
    <xsl:param name="replacement" select="''" /> 
    <xsl:param name="global"  select="false()" /> 

    <xsl:choose> 
    <xsl:when test="contains($subject, $search)"> 
     <xsl:value-of select="substring-before($subject, $search)" /> 
     <xsl:value-of select="$replacement" /> 
     <xsl:variable name="rest" select="substring-after($subject, $search)" /> 
     <xsl:choose> 
     <xsl:when test="$global"> 
      <xsl:call-template name="string-replace"> 
      <xsl:with-param name="subject"  select="$rest" /> 
      <xsl:with-param name="search"  select="$search" /> 
      <xsl:with-param name="replacement" select="$replacement" /> 
      <xsl:with-param name="global"  select="$global" /> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$rest" /> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$subject" /> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

這將是可調用爲:

<xsl:call-template name="string-replace"> 
    <xsl:with-param name="subject"  select="Name" /> 
    <xsl:with-param name="search"  select="'(MHC)'" /> 
    <xsl:with-param name="replacement" select="''" /> 
    <xsl:with-param name="global"  select="true()" /> 
</xsl:call-template>