Select distinct data from XML file. RRS feed

  • Question

  • User-893294271 posted
    I have an XML file with a list of names, IDs, and their bosses name. What i'm trying to do is find an XPath query that will select all of the distinct bosses from the XML file, similar to a SQL "select distinct boss from..." statement. I'm pretty new to this XML and XPath stuff, so any help would be appreciated, thanks!!
    Tuesday, March 25, 2003 9:29 AM

All replies

  • User2032526919 posted
    See my articles: Simple XSLT & XPath grouping http://www.aspalliance.com/joteke/xsltxpathgroupingarticle/article.aspx More XSLT & XPath grouping http://www.aspalliance.com/joteke/xsltxpathgroupingarticle2/article.aspx They will tell you how to do it.
    Tuesday, March 25, 2003 1:11 PM
  • User-893294271 posted
    I must be doing something wrong here, because I tried the examples show above, and I just keep getting "Missing operand before '/' operator" error. The line giving me the error is: objDataView.RowFilter="//User/User[not(@ED=preceding-sibling::User[1]/@ED)]/@ED" Any ideas what I'm doing wrong here?? Thanks!
    Tuesday, March 25, 2003 3:07 PM
  • User2032526919 posted
    You don't use XPath with DataView's RowFilter. It has nothing to do with it. RowFilter has it's own expression syntax that you can check from here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassexpressiontopic.asp (ittalks about DataColumn's Expression but they are same by the syntax) You would use XPath if you use XSLT to transform the source XML, or if you load data from XML using XmlDocument or XmlDataDocument.
    Wednesday, March 26, 2003 1:21 AM
  • User-169031426 posted
    You could use something like this to get distinct values and put them in a listbox or dropdown: Dim objXML As New XmlDocument() objXML.Load(Server.MapPath("/xml_files/test.xml")) 'dt would be the name of the datatable that the data resides 'and I just used bosses since that is what you are looking to get Dim nodes As XmlNodeList = objXML.SelectNodes("//dt[not(bosses= preceding-sibling::dt/bosses)]/bosses") Dim ienum As IEnumerator = nodes.GetEnumerator While ienum.MoveNext listbox1.Items.Add(New ListItem(CType(ienum.Current, XmlElement).ChildNodes(0).Value)) End While
    Thursday, March 27, 2003 1:33 PM
  • User-893294271 posted
    That works almost perfectly! The only thing I need to figure out is how to put the ID field in as the value, and the show the name as the text. Any suggestions??
    Thursday, March 27, 2003 3:01 PM
  • User-131563635 posted
    So there is no simple way to apply a "select distinct *" instead of usin XSLT/SPath etc.? I do not know a lot about XML/XSL etc. yet; all I would like to do is read a simple XML file that has category, product name, etc... and have a drop-down list show distinct categories. Isn't there some kind of DataView filter expression or something of the sorts?
    Tuesday, October 21, 2003 7:38 PM
  • User1794711221 posted
    Unfortunately your are right, in XPath/XSLT 1.0 there is no simple and pure way to select distinct nodes. In XSLT usually Muenchian grouping method is used [1]. In XPath some thicks like explained above. As well as EXSLT extension function set:distinct [2]. And good news - with recently released EXSLT.NET[3] library it's possible to select distinct nodes via set:distinct() function. Even in XPath-only environment, with no XSLT. It's way more effective and simpler than any other approaches: XPathExpression expr = nav.Compile("set:distinct(//author)"); expr.SetContext(new ExsltContext(doc.NameTable)); XPathNodeIterator authors = nav.Select(expr); while (authors.MoveNext()) Console.WriteLine(authors.Current.Value); Dare Obasanjo is preparing an article for his Extreme XML column on this topic, watch MSDN news. [1] http://www.jenitennison.com/xslt/grouping/ [2] http://www.exslt.org/set/functions/distinct/index.html [3]http://www.gotdotnet.com/Community/Workspaces/workspace.aspx?id=201eb290-5bf0-4452-9bf7-d21d39268f36
    Wednesday, October 22, 2003 11:55 AM