开发者

Getting distinct values ; ASP.NET

In a recent project, I was able to use the following syntax to get distinct values from my XML file:

<xsl:for-each select="distinct-values($ds/datasource/Products-list/Products/CategoryName)">

But now, we are migrating the project to ASP.NET and the following code does not work:

public DataSourceManager manager = new DataSourceManager();

protected void Page_Load(object sender, EventArgs e)
{
    this.manager.Get("http:***",
            "distinct-values(/datasource/Products-list/Products/CategoryName)", 
            new String[] { "." }, this.messageRepeater);
    }
}

The Get function looks like:

public void Get(String datasourceUrl,
                String xpathToNodes,
                Array nodeNames,
                Repeater repeater,
                params String[] options ) {

    Debug.WriteLine("datasourceUrl= " + datasourceUrl);
    Debug.WriteLine("xpathToNodes= " + xpathToNodes);

    //call datasource url
    XmlDocument doc = new XmlDocument();
    doc.Load(datasourceUrl);

    //statusCode
    this.statusCode = doc.SelectSingleNode("/datasource/result/status/@code").Value;

    if (options.GetLength(0) > 0) {
        this.maxItem = Convert.ToInt16(options[0]);
    }

    //iterate
    this.list = new ArrayList();
    int count = 0;
    if (IsErrorCode == false) {
        XmlNodeList nodes = doc.SelectNodes(xpathToNodes);
        foreach (XmlNode node in nodes) {
            Hashtable row = new Hashtable();
            foreach (String nodeName in nodeNames) 开发者_C百科{
                row.Add(nodeName, node.SelectSingleNode(nodeName).InnerText);
            }
            list.Add(row);

            if (++count == this.maxItem) {
                break;
            }
        }
    }

    //data binding
    repeater.DataSource = list;
    repeater.DataBind();
}

The error returned is the following:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

How can I get distinct values in ASP.NET?


I believe that the distinct-values function is only available in xslt 2.0. If you are doing anything with .NET built in XSLT functionality it only supports 1.0. This could explain your error.

Unfortunately selecting distinct records on XSLT 1.0 can be a bit of a pain. This SO post goes over a good method for doing this:

How to use XSLT to create distinct values


EDIT: Abe's answer looks like it's unfortunate but important. If you were using .NET 3.5 or higher I'd suggest using LINQ to XML instead, but if you're still using non-generic collections, it's not promising... it could be that the link in Abe's answer is your best hope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜