473,725 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting the result of an XPath query?

Is it possible to sort the result of an XPath "select"
query?

I need to return a subset of a large xml file, and XPath
is the way to go. I'd like to sort this subset afterwards?

This seems like a common task so should be quite trivial.
I've seen the same thing done but by first pumping the
subset into an XmlDocument then applying XSLT.

Is there an easier way, possibly working directly on the
XPathNodeIterat or?

Thanks

Ben Fidge
Nov 12 '05 #1
2 7049
Ben Fidge wrote:
Is it possible to sort the result of an XPath "select"
query?


Sure it's possible, here is MSDN sample on AddSort method of
XPathExpression class:

XPathDocument doc = new XPathDocument(" booksort.xml");
XPathNavigator nav = doc.CreateNavig ator();

//Select all books by Jane Austen.
XPathExpression expr;
expr = nav.Compile("de scendant::book[author/last-name='Austen']");

//Sort the selected books by title.
expr.AddSort("t itle", XmlSortOrder.As cending, XmlCaseOrder.No ne,
"", XmlDataType.Tex t);

//Display the selection.
XPathNodeIterat or iterator = nav.Select(expr );
while (iterator.MoveN ext()){
XPathNavigator nav2 = iterator.Curren t.Clone();
nav2.MoveToFirs tChild();
Console.WriteLi ne("Book title: {0}", nav2.Value);
}

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #2


Thanks Oleg,

I knew there must be a simple way of doing it.

Ben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2997
by: R | last post by:
Hello everybody. I'm new to XML and I've got problem with one XPath query. This is my situation: <group> <test required='1'></test> <test required='1' noregexp='1'></test> </group> inside group there are few (usually from 10 to 20) test nodes.
2
1751
by: David Gordon | last post by:
Hi Folks, I wonder if anyone can help me with the following (perhaps trivial) problem: <xml> <node name="a" type="a"/> <node name="b" type=""/> <node name="c"/> <node name="d" type="b"/> </xml>
3
2648
by: IMS.Rushikesh | last post by:
Hi Friends, I need a XPath query, whcih ll return me subset of data.... check below xml stream <label id="MyExpenseDetails_lbl" xlink:role="terseLabel">Short Expense Details</label> <label id="MyExpenseDetails_lbl" xlink:role="displayLabel">Expense Details</label> <label id="InternalExpense_lbl" xlink:role="displayLabel">Internal
2
1349
by: manmit.walia | last post by:
Hello, I have a XML File that looks something like this. <ItemList> <ProductType>Google</ProductType> <EmployeeName>John,X,Doe,john.doe@domain.com</EmployeeName> <EmployeeName>Jane,X,Doe,jane.doe@domain.com</EmployeeName> <EmployeeName>Larry,X,Doe,larry.doe@domain.com</EmployeeName> </ItemList> What I am trying to accomplish is do an XPath Query that finds all
2
10049
by: dc | last post by:
i have a xml file like this: <?xml version="1.0" encoding="utf-8"?> <validate xmlns="http://tempuri.org/fieldValidate.xsd"> <field name="Short Name" type="SN" length="10"> <requiredChar value="Y" errMsg="required Y" /> <requiredChar value="X" errMsg="required X" /> <bannedChar value="Z" errMsg="banned Z" /> <bannedChar value="A" errMsg="banned A" /> </field>
3
1504
by: cobus.lombard | last post by:
Hi All Couldn't find an xpath specifig UG, so I'm posting this here. I have XML in the following structure: <Assignments> <Assignment> <ID>123</ID> <Description>Test Assignment 1</Description>
3
4979
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml version="1.0" encoding="utf-8" ?> <thing xmlns="urn:thing-schema-v1"> <foo>foo thing</foo> <bar>bar thing</bar>
2
1546
by: =?Utf-8?B?RGlmZmlkZW50?= | last post by:
Hello All, I am trying to construct an XPath query against an XML document, for a requirement that I have. Below is the XML fragment: <SUBMISSIONS> <SUBMISSION YEAR="2004"> <MONTH NAME="JAN">10</MONTH> <MONTH NAME="FEB">02</MONTH>
5
2433
by: tschulken | last post by:
I have a query where i need to look for a value of a lower level xml element based on the value of a parent element existing first. Here is a simple example of the xml <S3Client> <Buttons> <Button>Activity <RestrictedClientType> <ClientType>02</ClientType> </RestrictedClientType>
1
2127
by: yawnmoth | last post by:
<?php $content = ' <a> <d> <b> <c/><c/> </b> </d> <b> <c/><c/><c/>
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.