473,811 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort a group of nodes by attribute? How?

Hi,

I have the following XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<items>
<item line="2">all that glitters is gold,</item>
<item line="1">Theres a lady thats sure,</item>
<item line="3">and she's buying a stairway to heaven</item>
</items>

To output this code Im using the following PHP:

$xmldoc = domxml_open_fil e('stairway.xml ',
DOMXML_LOAD_DON T_KEEP_BLANKS);
$node = $xmldoc->document_eleme nt();
if ($xmldoc->has_child_node s()) {
$node = $node->first_child( );
$endwhile = false;
while ($endwhile != true) {
echo $node->get_content( ) . "<br />";
if ($node->next_sibling() ) {
$node = $node->next_sibling() ;
}else{
$endwhile = true;
}
}
}

This does what it supposed to do except I want to ensure that it
outputs nodes ordered by 'line' attribute. The obvious answer would be
to put them in order in the XML file and the above example is very
simple so that you get the point, the real life example however will be
much larger and encase anyone put something in the wrong order it would
muck up my presentation. How could I adapt my code above to allow my
nodes to be sorted? I dont mind the nodes being out of order in the
physical file (stairway.xml) but when they are output using my script,
I want them to be ordered.

Thanks

Burnsy

Aug 11 '05 #1
1 1463
How about if I entered the node value and the date attribute into a
multi-dimensional array and sorted and then sorted it by date? I dont
really know a great deal about multi-dimensional arrays but ill get
learning.

I understand that it could probably be done using xslt but thats a step
ahead of me yet.

I thought that their would have been an easier way as when you xmldom
read from a xml file it creates a virtual xml tree where nodes can be
added, removed, updated and I figured even sorted before being
re-written to the xml file (if required).

Anyway, Ill probably just get onto the array idea. Thats should do it.
Please give comment if you know a better way or any suggestions on my
above idea. Thanks

Burnsy

Aug 11 '05 #2

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

Similar topics

18
6213
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn, date and several other bits of info too. I can do a simple sort over the first field (title as it turns out), and that is fine as far as it gets:
2
10704
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
3
5516
by: Alan | last post by:
Hi all, Besides the Sorted property, the TreeView control in .Net FCL doesn't provide application-defined sort. But I put several kinds of data into the treeview, and hope to sort them respectively. If setting its Sorted true, all nodes will be sorted. It's not my requirement. Any idea. alan
0
14861
by: Chris Bowring | last post by:
Alan, the easiest way to sort a TreeView is to: * add the nodes to an ArrayList * sort the ArrayList with an IComparer * remove the nodes from the TreeView and add them back from the ArrayList. e.g. in a sub-class of TreeView:
2
5156
by: jon|k | last post by:
hi all-- i need to do a transformation that removes duplicates (among other things). to accomplish that, i'm trying to use for-each-group, but it doesn't work. i need to select for duplicates by looking at the child node sequence (see sample below). note that when i do an xsl-message on the group-by expression inside the for-each-group, it has exactly what i'd like to group by listed, but i guess it doesn't like to have a sequence of...
2
1574
by: siromega | last post by:
Hello all, I'm having a problem with XSL:Sort. I've parameterized it (and use javascript to populate the parameters, but that part is working fine).. <xsl:sort select="*" /> The problem is that sometimes $sort_project_col is the name of a child element (that works fine), but sometimes it is also the name of an
2
2204
by: Patrick | last post by:
I got a page that loads a xml file into a treeview control. I want it to sort the data before sending it to the treeview control is that possible? <root> <level1 name="" src="" order="0"> <level1.2 .. order="5"> <level1.2 .. order="3"> <level1.2 .. order="2"> <level1 .. order="2"> <level1 .. order="1">
1
1674
by: karthikpv | last post by:
Hi, I am trying to group nodes in a huge xml file by one of the common fields. I have tried using "for-each-group" and "group-by" but am unable to get it working. Here, I have a list of Ports under each DSLAM entry and would hence like to group in a hierarchy all such Ports under each of these DSLAM entries as shown below.
0
13329
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a third array containing the age of a person; how to sort the
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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
10379
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
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
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
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.