2011-05-06 94 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>