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?
I believe that the
distinct-valuesfunction 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