XSLT extension methods must return a type that is supported within XSL transformations. The following table shows the W3C XPath types and their corresponging .NET type:
W3C XPath Type | Equivalent .NET Class (Type)
------------------------------------------------------
String | System.String
Boolean | System.Boolean
Number | System.Double
Result Tree Fragment | System.Xml.XPath.XPathNavigator
Node Set | System.Xml.XPath.XPathNodeIterator
The table is taken from the section Mapping Types between XSLT and .NET in this MSDN Magazine article.
Instead of returning a string[]
array you would have to return an XPathNodeIterator
like it is done in the following example:
<msxsl:script implements-prefix="extension" language="C#">
<![CDATA[
public XPathNodeIterator GetList(string str, string delimiter)
{
string[] items = str.Split(delimiter.ToCharArray(), StringSplitOptions.None);
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("root"));
using (XmlWriter writer = doc.DocumentElement.CreateNavigator().AppendChild())
{
foreach (string item in items)
{
writer.WriteElementString("item", item);
}
}
return doc.DocumentElement.CreateNavigator().Select("item");
}
]]>
</msxsl:script>
In your XSL transform you can then iterate over the elements in the returned node set using xsl:for-each
:
<xsl:template match="/">
<root>
<xsl:for-each select="extension:GetList('one,two,three', ',')">
<value>
<xsl:value-of select="."/>
</value>
</xsl:for-each>
</root>
</xsl:template>