Fio*_*ite 14 regex xml xslt xpath replace
我正在编写一个XSLT转换,我希望使用Replace函数进行正则表达式匹配和替换.
但是,Visual Studio 2008报告了这一点
'replace()'是一个未知的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>
Run Code Online (Sandbox Code Playgroud)
有什么我做错了吗?
谢谢 :)
编辑:我正在使用这个版本的XSLT,但看起来它是Visual Studio的版本是一个问题......我将不得不尝试找到一个解决方法.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Run Code Online (Sandbox Code Playgroud)
Wel*_*bog 31
该replace
功能仅适用于XSLT 2.0版,而不适用于Visual Studio使用的 1.0版本.仅仅因为你已经指定version="2.0"
并不意味着Visual Studio支持它.
这是关于在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>
Run Code Online (Sandbox Code Playgroud)
你这样称呼它:
<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>
Run Code Online (Sandbox Code Playgroud)
替换在XSLT 1.0中无效.你有"translate()",它可能适合你,但replace()是XSLT 2,而不是MS .NET XML代码库的一部分.您可以通过一些第三方XML库获取它.
如何嵌入ac#脚本来进行替换?
将以下内容添加到样式表的底部:
<msxsl:script language="C#" implements-prefix="scr">
<![CDATA[ public string Replace(string stringToModify, string pattern, string replacement) { return stringToModify.Replace(pattern, replacement); } ]]>
</msxsl:script>
将namespace属性添加到stylesheet元素:
xmlns:scr="urn:scr.this"
然后执行....
<xsl:value-of select="scr:Replace(description/text(), 'ABC', '123')"/>
Run Code Online (Sandbox Code Playgroud)