Re: Sort xml?
- From: "Patrick" <floods@xxxxxxxxxxx>
- Date: Thu, 22 Jun 2006 21:41:45 +0200
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/
.
- References:
- Sort xml?
- From: Patrick
- Re: Sort xml?
- From: Martin Honnen
- Sort xml?
- Prev by Date: Re: XML Config File
- Next by Date: Re: XML Config File
- Previous by thread: Re: Sort xml?
- Next by thread: selectNodes question
- Index(es):
Relevant Pages
|