具有C#扩展功能的XSLT中的错误消息 [英] Error message in XSLT with C# extension function

查看:82
本文介绍了具有C#扩展功能的XSLT中的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在XSLT中实现C#扩展功能时收到以下错误。

I am received the following error while trying to implement a C# extension function in XSLT.


扩展功能参数或返回值具有不支持CLR类型'Char []'。**

Extension function parameters or return values which have CLR type 'Char[]' are not supported.**

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?

编辑:我需要一个仍然可以实现split函数并利用返回的数组的解决方案。

I need a solution that still lets me implement the split function and make use of the array returned.

谢谢!

推荐答案

XSLT扩展方法必须返回XSL转换支持的类型。下表显示了W3C XPath类型及其对应的.NET类型:

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

该表摘自Xemt和.NET之间的映射类型部分。在此 MSDN杂志文章中。

The table is taken from the section Mapping Types between XSLT and .NET in this MSDN Magazine article.

与其返回 string [] 数组,不如返回一个 XPathNodeIterator ,而不是返回它在以下示例中完成:

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>

在XSL转换中,您可以使用<$ c $遍历返回的节点集中的元素c> xsl:for-each :

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>

这篇关于具有C#扩展功能的XSLT中的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆