473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sorting nodes

I have an XML document that contains a set of nodes with attributes.

As a stylised example, consider the following XML document:

oDoc.loadXML "<i><d a='1'/><d a='3'/><d a='4'/><d a='2'/><d a='0'/></i>"

What I want to do is set a nodelist to the <d/> elements and iterate through
them, printing out the attribute values.

For example (VB6 code):
'----------------------------------------
Dim oDoc As New MSXML2.DOMDocument
Dim oNodes As MSXML2.IXMLDOMNodeList
Dim oNode As MSXML2.IXMLDOMNode

oDoc.loadXML "<i><d a='1'/><d a='3'/><d a='4'/><d a='2'/><d a='0'/></i>"

Set oNodes = oDoc.selectNodes("//d")

For Each oNode In oNodes
Debug.Print oNode.Attributes.getNamedItem("a").Text
Next oNode
'----------------------------------------

However, what I REALLY want to do is to pre-order the nodes so that they are
in ascending (or descending) attribute value.

I'm thinking along the lines of using the SORT function on an ADO
recordset....

Any suggestions?

Thanks

Griff
Nov 12 '05 #1
2 3202
fairly easy if you know how to use XSL (this uses MSXML)

create the two files below and drop them into the same folder.

first the XML file...

<?xml-stylesheet type="text/xsl" href="SortingNodes.xsl"?>
<i>
<d a="1"/>
<d a="3"/>
<d a="4"/>
<d a="2"/>
<d a="0"/>
</i>

then the XSL file...

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="*">
<!-- loop thru the nodes in the first list -->
<xsl:for-each select="//d/@a">
<!-- sort the output -->
<xsl:sort select="." order="descending"/>
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<!-- if it's not the last item found, place a comma after it -->
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

you can then extract the values of each from the comma separated list.

you can also do the transform inside the VB program, and get the
results without having to use the browser.

ResultText = xmlDoc.transformNode(styleDoc)

look at the .transformNode method

Nov 12 '05 #2
Fantastic - I'll look into that.

The actual XML nodes have many attributes, so I'll create an XSL template
and programatically change the place holders to point at different attribute
names and change the ascending/descending values and then transform the XML.

Griff
Nov 12 '05 #3

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

Similar topics

0
by: Sparko | last post by:
My Xml is as follows; <root> <st id="1"> <rt id="1"> <cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs> <ct some attribs> <te some attribs /> <te some attribs />
6
by: Ksenia Marasanova | last post by:
Hi, I have a list that contains nodes from a tree. Each node is a class instance, but I'll use dictionary here to simplify the example. So the list looks like this: Where 'ord_number' is...
0
by: noureensyed | last post by:
Hi there, Any ideas on how to sort an xml of the type below. I want to output the exact same xml as well just sorted. However in my case I want to select a particular column tag below, say...
4
by: moital | last post by:
i am looking for a c# way to sort an xmlDocument notes the input of the function is a xml as string, and a path of the to be sorted nodes and the attribute to sort by as any one implement...
6
by: jw | last post by:
hi all,i have such 2 classes for a linked list, class Node{ private: int value; Node *next; public: Node() { next=NULL; }
4
by: R | last post by:
Hi All, I have problem with sorting nodes I want sort them by columns c1, c2 also I have node <root><sort ActiveField="c0"/></root> ActiveField attribute is set by user. I'm sorting 'row'...
6
by: Christoph | last post by:
I'm trying to come up with a stylesheet where, when the rows are displayed, duplicate game names are not shown on subsequent rows. It works but doesn't work properly. If I sort the data using...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
0
by: NickP | last post by:
Hi there, I am trying to sort a treeview control, which is owner drawn (if that makes any difference). Anyway, It only seems to be sorting the the root nodes for some reason. I have tested...
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.