2011-05-06 93 views
0

我想產品在XML文檔XSLT:XML:更換功能/功能

<text>words bli!wordsbli!</text> 

的BLI以下HTML

words<block>words</block> 

從這個節點!表示將在xml文檔的節點中隨機出現的標籤。是否有可能取代bli!使用xslt函數?

+0

你要替換'BLI'或''!? – 2011-05-06 18:55:39

+0

使用XSLT 2.0和正則表達式很容易。使用1.0比較困難。你正在使用哪個版本? – Lumi 2011-05-06 18:57:03

+0

@Michael Seal版本2.0 – William 2011-05-06 19:37:26

回答

1

XSLT 1.0沒有內置任何基本的字符串替換。

This post找到了一個實現此功能的好方法。

<xsl:template name="string-replace-all"> 
    <xsl:param name="text" /> 
    <xsl:param name="replace" /> 
    <xsl:param name="by" /> 
    <xsl:choose> 
     <xsl:when test="contains($text, $replace)"> 
     <xsl:value-of select="substring-before($text,$replace)" /> 
     <xsl:value-of select="$by" /> 
     <xsl:call-template name="string-replace-all"> 
      <xsl:with-param name="text" 
      select="substring-after($text,$replace)" /> 
      <xsl:with-param name="replace" select="$replace" /> 
      <xsl:with-param name="by" select="$by" /> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$text" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

下面是它怎麼叫:

<xsl:variable name="myVar"> 
    <xsl:call-template name="string-replace-all"> 
     <xsl:with-param name="text" select="'This is a sample text : {ReplaceMe} and {ReplaceMe}'" /> 
     <xsl:with-param name="replace" select="'{ReplaceMe}'" /> 
     <xsl:with-param name="by" select="'String.Replace() in XSLT'" /> 
    </xsl:call-template> 
    </xsl:variable>