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?

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
JFFF
  • 991
  • 4
  • 17
  • 31

2 Answers2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks. Did not notice the typo. I edited my post above with the code I believe could be helpful. Really appreciate the help. – JFFF Sep 06 '11 at 18:56
  • @JFFF: Any reason you're using the .NET 1.1 non-generic collections? What version of .NET are you using? – Jon Skeet Sep 06 '11 at 18:59
  • 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 Sep 06 '11 at 19:01
  • @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. – Jon Skeet Sep 06 '11 at 19:08
  • Ok. I'll take it from here. Thanks for the help. – JFFF Sep 06 '11 at 19:09
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

Community
  • 1
  • 1
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486