Reputation: 1194
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.
Upvotes: 0
Views: 1248
Reputation: 167706
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 do
<xsl:value-of select="'lorem', 'ipsum', 'dolor', 'sit', 'amet'" separator=""/>
in XSLT 2.0 or also
<xsl:value-of select="string-join(('lorem', 'ipsum', 'dolor', 'sit', 'amet'), '')"/>
Upvotes: 1