带参数的 C# XSLT 帮助程序
我有 xsl 视图引擎,想要从 xsl 调用标准视图助手(例如 UrlHelper.Action)。但我未能将参数的变量成员传递给辅助方法。
帮助程序类:
namespace Services
{
public class ViewHelper
{
// ...
public string DummyHelper(params string[] dummyArgs)
{
return String.Concat(dummyArgs);
}
}
}
向 xsl 转换添加帮助程序支持:
var xsl = new XslCompiledTransform();
xsl.Load('MyView.xsl');
varc xsltArgs = new XsltArgumentList();
// Create helper, pass controller context as a param
var helper = new Services.ViewHelper(context));
xslt.AddExtensionObject("urn:helper", helper);
xsl.Transform(xmlDocument, xsltArgs, output);
使用帮助程序的 xsl 脚本之一:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:h ="urn:helper"
exclude-result-prefixes="h msxsl"
>
<xsl:template match="/">
<xsl:value-of select="h:DummyHelper('lorem', 'ipsum', 'dolor', 'sit', 'amet')"/>
</xsl:template>
</xsl:stylesheet>
引发异常并显示消息:找不到带有 5 个参数的 mthod DummyHelper。
I have xsl view engine and want to call standard view helpers (e.g. UrlHelper.Action) from xsl. But I failed to pass variable nembers of params to the helper methods.
The helper class:
namespace Services
{
public class ViewHelper
{
// ...
public string DummyHelper(params string[] dummyArgs)
{
return String.Concat(dummyArgs);
}
}
}
Adding helper support to xsl transformation:
var xsl = new XslCompiledTransform();
xsl.Load('MyView.xsl');
varc xsltArgs = new XsltArgumentList();
// Create helper, pass controller context as a param
var helper = new Services.ViewHelper(context));
xslt.AddExtensionObject("urn:helper", helper);
xsl.Transform(xmlDocument, xsltArgs, output);
One of xsl scripts that uses helper:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:h ="urn:helper"
exclude-result-prefixes="h msxsl"
>
<xsl:template match="/">
<xsl:value-of select="h:DummyHelper('lorem', 'ipsum', 'dolor', 'sit', 'amet')"/>
</xsl:template>
</xsl:stylesheet>
The exception raises with message that says: cannot find mthod DummyHelper with 5 args.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么文档位于 http://msdn.microsoft.com/en-us/library /tf741884.aspx 明确表示“当前不支持使用 params 关键字定义的任何方法,该方法允许传递未指定数量的参数” XslCompiledTransform 类。”所以你想要的不被支持。根据您的需求,您可以考虑是否可以使用纯 XSLT 2.0 和 .NET XSLT 2.0 处理器之一(例如 Saxon)来实现该功能9 或 XQSharp。 XSLT 2.0 比 XSLT 1.0 强大得多,允许您在纯 XSLT 中使用
xsl:function
编写函数。对于您的示例,您可以简单地使用 XSLT 2.0 或也可以
Well the documentation at http://msdn.microsoft.com/en-us/library/tf741884.aspx clearly says "Any method that is defined with the params keyword, which allows an unspecified number of parameters to be passed, is not currently supported by the XslCompiledTransform class." so what you want is not supported. Depending on your needs you could consider whether implementing the functionality is possible with pure XSLT 2.0 and one of the .NET XSLT 2.0 processors like Saxon 9 or XQSharp. XSLT 2.0 is a lot more powerful than XSLT 1.0 and allows you to write functions with
xsl:function
in pure XSLT. For your sample you could simply doin XSLT 2.0 or also