HighTechTalks DotNet Forums  

Xpath and Sorting

Dotnet XML microsoft.public.dotnet.xml


Discuss Xpath and Sorting in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Lou
 
Posts: n/a

Default Xpath and Sorting - 11-20-2007 , 10:23 AM






How do I create an xpath expression to return a sorted nodelist?
I need the sort to be by the attribute value.
Example, If I have an xml list of names with "lastName" as an attribute
<name lastName="Smith" />
<name lastName="Brown" />

i want to get back a sorted nodelist

Dim NodeSorted as
xmlNodeList=NodeNameList.SelectNodes("Name[@lastName=sorted"]
or something to this effect

-Lou



Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Xpath and Sorting - 11-20-2007 , 10:48 AM






Lou wrote:
Quote:
How do I create an xpath expression to return a sorted nodelist?
See <URL:http://msdn2.microsoft.com/en-us/library/b2hhe5h7.aspx> for the
AddSort method on XPathExpression.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Xpath and Sorting - 11-20-2007 , 11:20 AM



Martin Honnen wrote:
Quote:
Lou wrote:
How do I create an xpath expression to return a sorted nodelist?

See <URL:http://msdn2.microsoft.com/en-us/library/b2hhe5h7.aspx> for the
AddSort method on XPathExpression.
Here is an example:

Dim doc As XPathDocument = New XPathDocument("..\..\XMLFile1.xml")
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim ex1 As XPathExpression = XPathExpression.Compile("*/name")
ex1.AddSort("@lastName", XmlSortOrder.Ascending,
XmlCaseOrder.LowerFirst, "en", XmlDataType.Text)
Dim it As XPathNodeIterator = nav.Select(ex1)
While it.MoveNext()
Console.WriteLine(it.Current.GetAttribute("lastNam e", ""))
End While



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #4  
Old   
Lou
 
Posts: n/a

Default Re: Xpath and Sorting - 11-20-2007 , 11:27 AM



Maybe you could help me, I can't get it to work
here is my xml

<xml>
<names>
<name lastName="Smith" />
<name lastName="Brown" />
</names>
</xml>

What is the expresiion I use as Expression1 in the example to sort by
lastName attribute you linked me too?
Also does it return a Node?

Here is my code where the iterator.Current.Value returns empty strings.
=================================================
'Construct the XPathDocument by specifying the path to Books.xml.

Dim xmldoc As New System.Xml.XPath.XPathDocument(ElectionData.DataFi le)

'Create XPathNavigator.

Dim nav As System.Xml.XPath.XPathNavigator = xmldoc.CreateNavigator()

'Compile the XPath query expression to select all the Title elements.

'The Compile method of the XPathNavigator generates an XPathExpression

'object that encapsulates the compiled query.

Dim expr As System.Xml.XPath.XPathExpression =
nav.Compile("xml/rnames/name/[@lastName]")

'Execute the AddSort method of the XPathExpression object to define the

' Title Element as the sort key.

expr.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "", Xml.XPath.XmlDataType.Text)



'Create the XPathNodeIterator by executing the Select method of the
XPathNavigator.

'Notice that the XPathExpression object is supplied as the query expression
parameter.

Dim iterator As System.Xml.XPath.XPathNodeIterator = nav.Select(expr)

System.Diagnostics.Debug.WriteLine("Titles Sorted in Ascending Order")

System.Diagnostics.Debug.WriteLine("************** ******************")

'Use the iterator to explore the result set that is generated.

Do While iterator.MoveNext

System.Diagnostics.Debug.WriteLine(iterator.Curren t.Value)

Loop




"Martin Honnen" <mahotrash (AT) yahoo (DOT) de> wrote

Quote:
Lou wrote:
How do I create an xpath expression to return a sorted nodelist?

See <URL:http://msdn2.microsoft.com/en-us/library/b2hhe5h7.aspx> for the
AddSort method on XPathExpression.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/



Reply With Quote
  #5  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Xpath and Sorting - 11-20-2007 , 12:02 PM



Lou wrote:

Quote:
Here is my code where the iterator.Current.Value returns empty strings.
=================================================
'Construct the XPathDocument by specifying the path to Books.xml.

Dim xmldoc As New System.Xml.XPath.XPathDocument(ElectionData.DataFi le)

'Create XPathNavigator.

Dim nav As System.Xml.XPath.XPathNavigator = xmldoc.CreateNavigator()

'Compile the XPath query expression to select all the Title elements.

'The Compile method of the XPathNavigator generates an XPathExpression

'object that encapsulates the compiled query.

Dim expr As System.Xml.XPath.XPathExpression =
nav.Compile("xml/rnames/name/[@lastName]")
That does not make sense as an XPath expression use
nav.Compile("xml/names/name")
then set the sort as
Quote:
'Execute the AddSort method of the XPathExpression object to define the

' Title Element as the sort key.

expr.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "", Xml.XPath.XmlDataType.Text)
expr.AddSort("@lastName", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "en", Xml.XPath.XmlDataType.Text)

As an alternative approach you could use
nav.Compile("xml/names/name/@lastName")
then you can use
expr.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "en", Xml.XPath.XmlDataType.Text)
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #6  
Old   
Lou
 
Posts: n/a

Default Re: Xpath and Sorting - 11-20-2007 , 12:08 PM



Thanks, your a genius. That saved me SOOOOOO much time!
-Lou

"Martin Honnen" <mahotrash (AT) yahoo (DOT) de> wrote

Quote:
Martin Honnen wrote:
Lou wrote:
How do I create an xpath expression to return a sorted nodelist?

See <URL:http://msdn2.microsoft.com/en-us/library/b2hhe5h7.aspx> for the
AddSort method on XPathExpression.

Here is an example:

Dim doc As XPathDocument = New XPathDocument("..\..\XMLFile1.xml")
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim ex1 As XPathExpression = XPathExpression.Compile("*/name")
ex1.AddSort("@lastName", XmlSortOrder.Ascending,
XmlCaseOrder.LowerFirst, "en", XmlDataType.Text)
Dim it As XPathNodeIterator = nav.Select(ex1)
While it.MoveNext()
Console.WriteLine(it.Current.GetAttribute("lastNam e", ""))
End While



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.