C# 在xslt中进行替换的最佳(更快)方法是什么
在xslt中进行替换的最佳(更快)方法是什么 1/带模板C# 在xslt中进行替换的最佳(更快)方法是什么,c#,xml,xslt,C#,Xml,Xslt,在xslt中进行替换的最佳(更快)方法是什么 1/带模板 <xsl:template name="str-replace"> <xsl:param name="text" /> <xsl:param name="replace" /> <xsl:param name="by" /> <xsl:choose> <xsl:when test="contains($text, $repla
<xsl:template name="str-replace">
<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="str-replace">
<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>
2/带有扩展对象
public class ToolBox
{
public string replace(string s, string el, string by)
{return s.Replace(el, by);}
}
<xsl:value-of select="toolbox:replace($foo,$bar, $fobar)" />
公共类工具箱
{
公共字符串替换(字符串s、字符串el、字符串by)
{返回s.Replace(el,by);}
}
为了直接回答您的问题,我希望字符串操作方法的性能优于XML操作,因为它不必解析XML文档
但是,您应该考虑是否希望替换程序对文档的XML性质敏感。即,您是否只想:
字符串替换为
将改变起始
元素,但不会改变结束
元素性能问题取决于是否了解您正在使用的产品。本机代码几乎肯定会更快,但在某些处理器上,调用扩展函数的开销很高。所以测量一下
或者切换到XSLT 2.0。您的
replace
函数在直接调用String.replace
上没有添加任何内容。确切地说,这不是问题……因此它不是作为答案发布的。据我所知,建议不要在未解析的词法XML上使用replace(),而是从XSLT调用在文本节点上使用replace()的扩展函数。但我可能把它解释错了!