Xslt 中调用C#代码
在xslt中调用c#代码,要用如下标签:
<msxsl:script language="language-name" implements-prefix="prefix of use's name">
</msxsl:script>
说明:
language:
用于此元素中定义的函数的活动脚本语言。如果未指定,将使用 Microsoft® JScript®(与 ECMA 262 语言规范兼容)。此属性接受的值与 HTML <SCRIPT> 元素的 language 属性相同。对于托管代码,此属性接受 CreateProvider 所接受的语言名称。
implements-prefix:必选项。将前缀与脚本块关联。此属性的值是表示命名空间的前缀。
父元素:xsl:stylesheet,xsl:transform
子元素:<msxsl:assembly>元素,<msxsl:using>元素
因为在xslt中用c#的时候默认的命名空间只有:
System
System.Collection
System.Text
System.Text.RegularExpressions
System.Xml
System.Xml.Xsl
System.Xml.XPath
如果想引用不在上面的命名空间中的方法,必须把对应的assembly引用进来。如要用HtmlEncode方法,具体例子如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts">
<msxsl:script language="C#" implements-prefix="user">
<msxsl:assembly name="System.Web"/>
<msxsl:using namespace="System.Web"/>
<![CDATA[
public double circumference(double radius){
double pi = 3.14;
double circ = pi*radius*2;
return circ;
}
public string HtmlEncode(string strTest)
{
return System.Web.HttpUtility.HtmlEncode(strTest);
}
]]>
</msxsl:script>
<xsl:template match="data">
<circles>
<xsl:value-of select="user:HtmlEncode(‘aaa’)"/>
<xsl:for-each select="circle">
<circle>
<xsl:copy-of select="node()"/>
<circumference>
<xsl:value-of select="user:circumference(radius)"/>
</circumference>
</circle>
</xsl:for-each>
</circles>
</xsl:template>
</xsl:stylesheet>
参考文档:
http://msdn.microsoft.com/zh-cn/library/ms256042(v=VS.80).aspx
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步