具有可变数量参数的 XSLT 扩展方法
我正在尝试实现具有可变数量参数的 XSLT 扩展函数。如果我将参数声明为数组、params 数组、ICollection 等,则会收到以下错误(其中 TYPE_NAME 是使用的类型):
System.Xml.Xsl.XslTransformException:具有 Clr 类型的扩展函数参数或返回值不支持“TYPE_NAME”。
但 Umbraco 有一个内置函数 concat
就是以这种方式运行的。我查看了 Umbraco 的源代码,它看起来像这样:
public static string concat(XPathNodeIterator nodeset){...}
在 XSLT 中它是这样调用的:
concat('a', 'b', 'c')
当我尝试类似的方法并按如下方式声明我的函数时:
public static string Test(XPathNodeIterator nodeset){...}
并以相同的方式在 XSLT 中调用它:
Custom:Test('a', 'b', 'c')
我收到以下错误:
System.Xml.Xsl.XslTransformException:扩展对象“urn:Custom”不包含具有 3 个参数的匹配“Test”方法。
我猜 concat 的输入在 XPathNodeIterator 中以某种方式进行了转换,但我无法弄清楚如何转换。有什么建议吗?
I'm trying to implement an XSLT extension function with variable number of arguments. If I declare the parameter as an array, a params array, a ICollection, etc. I get the following error (where TYPE_NAME is the type used):
System.Xml.Xsl.XslTransformException: Extension function parameters or return values which have Clr type 'TYPE_NAME' are not supported.
But the Umbraco has a built-in function concat
which functions in this way. I looked in Umbraco's source and it looks like this:
public static string concat(XPathNodeIterator nodeset){...}
and in XSLT it is called like this:
concat('a', 'b', 'c')
When I try a similar approach and declare my function as follows:
public static string Test(XPathNodeIterator nodeset){...}
and call it in XSLT in the same way:
Custom:Test('a', 'b', 'c')
I get the following error:
System.Xml.Xsl.XslTransformException: Extension object 'urn:Custom' does not contain a matching 'Test' method that has 3 parameter(s).
I guess the input of concat is somehow transformed in XPathNodeIterator but I am unable to figure how. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在执行的操作存在两个问题:
params
关键字的方法作为扩展函数,如 .NET 文档:.2.仅允许某些类型作为扩展函数的参数。这些定义在此处:
There are two problems with what you are doing:
params
keyword are not supported as extension functions, as specified in the .NET documentation:.2. Only certain types are allowed as parameters for extension functions. These are defined here: