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

 

http://msdn.microsoft.com/zh-cn/site/aa735782 

转载于:https://www.cnblogs.com/xiaosa_25/archive/2011/12/09/xslt_1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值