1

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) {
                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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

5 Comments

Thanks. Did not notice the typo. I edited my post above with the code I believe could be helpful. Really appreciate the help.
@JFFF: Any reason you're using the .NET 1.1 non-generic collections? What version of .NET are you using?
Honestly, I don't now. The reason why I'm asking for help here is because the person who wrote the code above is sick today, and I need to complete his work but I am kinda lost, considering I have not been part of the development of this project. If you believe my problem cannot be solved without further explanations from my side, I don't think I can provide them :(
@JFFF: I've edited my answer - if you can even find out the version of .NET you're using, that would really help. Otherwise I think Abe's answer is the most appropriate one.
Ok. I'll take it from here. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.