Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help please!! Sorting new data in an XML doc

Status
Not open for further replies.

andre2

Technical User
May 10, 2001
4
0
0
US
I really hope someone can help me, because I've posted this to 4 forums with no avail.

I'm using an ASP script with the Microsoft XML DOM to append HTML form data to an XML document.

I am using this as a news submitting system for my website. The problem is this - new entries, which are contained in the element <news> are appended to the bottom of the XML file, with older entries on top.
Obviously, it doesn't make sense for a user looking for news to scroll down to the bottom of the page to get the newest news! ;-) So how would I tell the script to insert new <news> entries at top above older <news> entries?
 
XML usually doesn't care about the order of the elements in a document (those that are siblings). You probably can't do this. Unless you were to add a sequence number to each element (as an attribute, perhaps?), and sort the elements before displaying them.

Chip H.
 
You probably have to re-create the entire XML document, first sorting the data in array or such.
 
by applying an XSL stylesheet you can order the nodes using the order-by attribute in the for-each...
ex.

If this is the XML
<?xml version=&quot;1.0&quot;?>
<root>
<childNode>
<firstName>Bob</firstName>
</childNode>
<childNode>
<firstName>Tony</firstName>
</childNode>
</root>

The xsl would contain
<xsl:for-each select=&quot;root/childNode&quot; order-by=&quot;firstName&quot;>

it defaults to Decending order but you can change the path by adding a + or - (ie. -firstName, +firstName)

When you get into dates it's a little more ugly due to the fact that xml in it's raw form is text only... there really are no data types. Microsoft has created a data definition file for defining datatypes. so the xml file would change to <firstName dt:dt=&quot;string&quot;>Bob</firstName> or in the case of your news <date dt:dt=&quot;dateTime&quot;>2000-01-29T12:00:00</date>

That is the format XML understands as a date if you were to use the MS data type format... go to msdn.microsoft.com and search on dt:dt you should find everything you need.

-B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top