Re: Sort xml?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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" <mahotrash@xxxxxxxx> wrote in message
news:O3eorkGlGHA.4888@xxxxxxxxxxxxxxxxxxxxxxx


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/


.



Relevant Pages

  • Re: Sort xml?
    ... An XSLT stylesheet can sort XML documents, for instance if the input XML is e.g. ...
    (microsoft.public.dotnet.xml)
  • Re: Illegal Charaters in path
    ... In the sample code I used the LoadXml first and got Data at the root ... Then I tried load, well I ... I have a simple XML ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to parse a HUGE(1gb) xml file
    ... Almost all exmaples pf parsing xml in python, i have seen, start off with these 4 lines of code. ... It's much faster and much more memory friendly than the Python implementation. ... root = tree.getroot#my huge xml has 1 root at the top level ...
    (comp.lang.python)
  • Re: Help: I cannot selectNodes with a simple XPath expression
    ... % [dom parse $xml] documentElement root ... So without an anonymous namespace, ...
    (comp.lang.tcl)
  • Re: Retrieving and Combining XML
    ... In SQL Server 2005, you can use ROOTin the FOR XML clause. ... the root element added on the client. ... I managed to make it work by adding a parent node in my ...
    (microsoft.public.sqlserver.xml)