2009-07-01 73 views
12

我正在編寫一個XSLT轉換,我希望使用Replace函數來執行正則表達式匹配和替換。找不到XSLT替換函數

但是,Visual Studio 2008的報告,

'取代()' 是未知的XSLT的功能。

的代碼本身的位是:

<xsl:otherwise> 
    <td style="border: solid 1px black; background-color:#00CC66;"> 
      <xsl:variable name="FeatureInfo" select="Text" /> 
       <xsl:value-of select="replace($FeatureInfo,'Feature=','TESTING')"/> 
    </td> 
</xsl:otherwise> 

有什麼,我做錯了什麼?

謝謝:)

編輯:我使用這個版本的XSLT,但看起來它是Visual Studio的版本,這是一個問題......我得設法找到一個解決辦法。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

回答

30

replace函數僅適用於XSLT版本2.0,不適用於版本1.0 which is what Visual Studio uses。僅僅因爲你指定了version="2.0"並不意味着Visual Studio支持它。

Here's a template on codesling that implements string-replace in XSLT 1.0。你應該可以使用它,但我不能保證它的效率。

(從上面的鏈接獲取)

<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:otherwise> 
    <td style="border: solid 1px black; background-color:#00CC66;"> 
    <xsl:variable name="FeatureInfo" select="Text" /> 
    <xsl:call-template name="string-replace-all"> 
     <xsl:with-param name="text" select="$FeatureInfo"/> 
     <xsl:with-param name="replace" select="Feature="/> 
     <xsl:with-param name="by" select="TESTING"/> 
    </xsl:call-template> 
    </td> 
</xsl:otherwise> 
+3

你需要在你的中加入單引號,比如select =「'TESTING'」。 – 2011-03-22 19:04:04

9

替換在XSLT 1.0中無效。你有「translate()」,這可能適合你,但是replace()是XSLT 2,而不是MS .NET XML代碼庫的一部分。不過,您可以通過一些第三方XML庫獲取它。

+1

正是我在爲我的項目尋找的東西。謝謝! – 2009-11-13 19:50:01

+0

完美,謝謝你:D – codemania23 2018-01-30 11:42:24

-1

據我所知,replace()在XLST 2.0引入。文檔的版本定義是什麼?也許你必須將VS 2008設置爲使用XLST 2.0(如果可能的話)。

0

你應該放在功能=引號之間的字符串如下

<xsl:otherwise><td style="border: solid 1px black; background-color:#00CC66;"> <xsl:variable name="FeatureInfo" select="Text" /> <xsl:call-template name="string-replace-all">  <xsl:with-param name="text" select="$FeatureInfo"/>  <xsl:with-param name="replace" select="'Feature='"/>  <xsl:with-param name="by" select="TESTING"/> </xsl:call-template> </td></xsl:otherwise> 

Thanks 
4

對於簡單的字符串替換的翻譯功能(在XSLT 1.0可用)爲我工作的罰款。

我用它去掉數值的空格。

4

如何嵌入c#腳本來做替換?

以下添加到您的樣式表的底部:

<msxsl:script language="C#" implements-prefix="scr"> <![CDATA[ public string Replace(string stringToModify, string pattern, string replacement) { return stringToModify.Replace(pattern, replacement); } ]]> </msxsl:script>

添加一個命名空間屬性的樣式表元素:

xmlns:scr="urn:scr.this"

然後實現爲....

<xsl:value-of select="scr:Replace(description/text(), 'ABC', '123')"/>