By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,825 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,825 IT Pros & Developers. It's quick & easy.

Sort xml?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.