Error message in XSLT with C # extension function

When trying to implement the C # extension function in XSLT, the following error was received.

Extension function parameters or return values ​​of the CLR type 'Char []' are not supported. **

the code:

<xsl:variable name="stringList">
  <xsl:value-of select="extension:GetList('AAA BBB CCC', ' ')"/>
</xsl:variable> 

<msxsl:script language="C#" implements-prefix="extension">
<![CDATA[

public string[] GetList(string str, char[] delimiter)
{
   ...
   ...
   return str.Split(delimiter, StringSplitOptions.None);
}

]]>
</msxsl:script>

Can someone explain this error message and how to get past it?

EDIT: I need a solution that allows me to implement the split function and use the returned array.

Thank!

+5
source share
2 answers

XSLT extension methods must return the type supported in XSL transforms. The following table shows the W3C XPath types and their suitable .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

XSLT .NET MSDN Magazine.

, string[], XPathNodeIterator, :

<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>

XSL node, 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>
+6

http://msdn.microsoft.com/en-us/library/533texsx(VS.71).aspx

, script, W3C XPath XSLT. W3C, .NET() W3C - XPath XSLT.

0

All Articles