Link to home
Start Free TrialLog in
Avatar of tesmc
tesmcFlag for United States of America

asked on

XML: how to sort a dom by attribute

I have the following input xml that I want to sort by /root/Traveler/@Type
I'm not concerned with ordering of the LastName or FirstName, precedence is attribute Type.

<root>
	<Traveler Type="ADT" AscID="T1">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>SELENA</FirstName>
		</TravelerName>
	</Traveler>
	<Traveler AscID="T2" Type="CNN">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>RACHAEL</FirstName>
		</TravelerName>
	</Traveler>
	<Traveler AscID="T3" Type="CNN">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>ANDREW</FirstName>
		</TravelerName>
	</Traveler>
	<Traveler AscID="T4" Type="ADT">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>SALMA</FirstName>
		</TravelerName>
		<Infant AscID="T4.1" Type="INF">
			<LastName>DUFFY</LastName>
			<FirstName>BABY</FirstName>
		</Infant>
	</Traveler>
</root>

Open in new window


Expected result:

<root>
	<Traveler Type="ADT" AscID="T1">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>SELENA</FirstName>
		</TravelerName>
	</Traveler>
	<Traveler AscID="T4" Type="ADT">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>SALMA</FirstName>
		</TravelerName>
		<Infant AscID="T4.1" Type="INF">
			<LastName>DUFFY</LastName>
			<FirstName>BABY</FirstName>
		</Infant>
	</Traveler>
	<Traveler AscID="T2" Type="CNN">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>RACHAEL</FirstName>
		</TravelerName>
	</Traveler>
	<Traveler AscID="T3" Type="CNN">
		<TravelerName>
			<LastName>DUFFY</LastName>
			<FirstName>ANDREW</FirstName>
		</TravelerName>
	</Traveler>
</root>

Open in new window


I had tried doing the below but it did not always worked, it seems to have taken into account the TravlerName

    var nodeComplete = domReq.Root.SelectSingle("CompletePNRElements");
    domReq.SortNodes(nodeComplete, "Traveler", "@Type", "XMLDOM_SORT_ASCENDING");
Avatar of tesmc
tesmc
Flag of United States of America image

ASKER

@leakim971 - thank you for the reference. from my initial post I indeed used that but it didn't sort my example above:

var nodeComplete = domReq.Root.SelectSingle("CompletePNRElements");
    domReq.SortNodes(nodeComplete, "Traveler", "@Type", "XMLDOM_SORT_ASCENDING");

would you have another suggestion?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tesmc

ASKER

@leakim971
thank you. i had my attribute reference incorrect but once i did as you noted "./@Type" it worked.
appreciate it.