473,289 Members | 1,723 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,289 software developers and data experts.

Sort xml?

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">

I want first level1 nodes to be sorted accorded to the order column. Then i
want all sub levels to be sorted, sor 1.2 should be sorted with only 1.2 and
1.3 should be sorte with only 1.3.

Like a window folder, in one folder you can sort the items in any order you
want, then when you move to the next folder you can sort that one.

Is it possible or do you have to rewrite the xml file, change the position
of line 3 and 4 with read/write, in order to change how they are viewed

This is the code witch i load the treeview control:
(this code also adds a node beneath every node with the text "Ny sida
....(new page)")

protected void Page_Load(object sender, EventArgs e)
{

TreeNode mNode;
TreeNode mNode2;
XmlDocument mdoc = new XmlDocument();
mdoc.Load("struct.xml");

page_treeview.Nodes.Clear();
mNode = new TreeNode(mdoc.DocumentElement.Name, "",
Image_Path_To_Rootimage);
page_treeview.Nodes.Add(mNode);

TreeNode tNode = new TreeNode();
tNode = page_treeview.Nodes[0];

AddNode(mdoc.DocumentElement, tNode);
mNode2 = new TreeNode("Ny sida...", "n0");

page_treeview.Nodes.AddAt(1, mNode2);

page_treeview.ExpandAll();
page_treeview.CollapseAll();
}

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
TreeNode nNode;
int i;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new
TreeNode(xNode.Attributes["name"].Value, xNode.Attributes["id"].Value,
Image_Path_To_Pageimages));

nNode = new TreeNode();
nNode.Text = "Ny sida...";
nNode.Value = "n" + xNode.Attributes["id"].Value;
inTreeNode.ChildNodes.Add(nNode);

tNode = inTreeNode.ChildNodes[i*2];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so
forth.
inTreeNode.Text = inXmlNode.Attributes["name"].Value;
inTreeNode.Value = inXmlNode.Attributes["id"].Value;
inTreeNode.ImageUrl = Image_Path_To_Pageimages;

nNode = new TreeNode();
nNode.Text = "Ny sida...";
nNode.Value = "n" + inXmlNode.Attributes["id"].Value;
inTreeNode.ChildNodes.Add(nNode);
}
}

Patrick
Jun 20 '06 #1
2 2175


Patrick wrote:
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">

I want first level1 nodes to be sorted accorded to the order column. Then i
want all sub levels to be sorted, sor 1.2 should be sorted with only 1.2 and
1.3 should be sorte with only 1.3.


An XSLT stylesheet can sort XML documents, for instance if the input XML
is e.g.
<root>
<level1 name="" src="" order="0">
<level1.2 order="5" />
<level1.2 order="3" />
<level1.2 order="2" />
</level1>
<level1 order="2" />
<level1 order="1" />
</root>

then this stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="level1">
<xsl:sort select="@order" data-type="number" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="level1">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="level1.2">
<xsl:sort select="@order" data-type="number" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="level1.2">
<xsl:copy>
<xsl:apply-templates select="@*" />
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

creates this sorted output

<?xml version="1.0" encoding="UTF-8"?>
<root>
<level1 name="" src="" order="0">
<level1.2 order="2"/>
<level1.2 order="3"/>
<level1.2 order="5"/>
</level1>
<level1 order="1"/>
<level1 order="2"/>
</root>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 20 '06 #2
dosent that require me to rewrite the xslt stylesheet everytime?
lets say that my xml list is like:
root
-level1
-level1.1
-level1.1.1
-level1.1.1.1
-level1.1.2
-level1.1.2.1
-level1.1.2.1.1

Can you programaticly change the xslt file? or can you use a loop in it
somehow?

-Patrick

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:O3**************@TK2MSFTNGP02.phx.gbl...


Patrick wrote:
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">

I want first level1 nodes to be sorted accorded to the order column. Then
i want all sub levels to be sorted, sor 1.2 should be sorted with only
1.2 and 1.3 should be sorte with only 1.3.


An XSLT stylesheet can sort XML documents, for instance if the input XML
is e.g.
<root>
<level1 name="" src="" order="0">
<level1.2 order="5" />
<level1.2 order="3" />
<level1.2 order="2" />
</level1>
<level1 order="2" />
<level1 order="1" />
</root>

then this stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="level1">
<xsl:sort select="@order" data-type="number" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="level1">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="level1.2">
<xsl:sort select="@order" data-type="number" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="level1.2">
<xsl:copy>
<xsl:apply-templates select="@*" />
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

creates this sorted output

<?xml version="1.0" encoding="UTF-8"?>
<root>
<level1 name="" src="" order="0">
<level1.2 order="2"/>
<level1.2 order="3"/>
<level1.2 order="5"/>
</level1>
<level1 order="1"/>
<level1 order="2"/>
</root>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 22 '06 #3

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

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
5
by: neocortex | last post by:
Hello! I am a newbie in Python. Recently, I get stuck with the problem of sorting by two criteria. In brief, I have a two-dimensional list (for a table or a matrix). Now, I need to sort by two...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.