Re: [xsl] How to sort Using Dom and SelectNodes

Subject: Re: [xsl] How to sort Using Dom and SelectNodes
From: "Malcolm Macaulay" <malcolm_xslt_list@xxxxxxxxxxx>
Date: Thu, 5 Jun 2003 15:49:28 -0500
Hi Karl,

With MSXML there is no method to sort nodes within the DOM. Your best bet
would be to create a simple XSLT (see below) to order the doc. The use code
something like:

var xmlUnsortedDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var xmlSortedDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var xmlSortXSLT = new ActiveXObject("Msxml2.DOMDocument.4.0");

... code to load the XML into xmlUnsortedDoc ...
... code to load the XSLT (see below) into xmlSortXSLT

// tranform the unsorted doc and stuff the resulting XML into a second DOM
xmlOrderedDoc.loadXML(xmlUnorderedDoc.transformNode(xmlSortXSLT));

... then get the required nodes with .selectNodes method

This XSLT should do the trick.

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

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

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*">
    <xsl:sort select="@order" data-type="number" order="ascending"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

Hope that helps

Malcolm


----- Original Message ----- 
From: "Rick Taylor" <taylor@xxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, June 05, 2003 1:55 PM
Subject: Re: [xsl] How to sort Using Dom and SelectNodes


> I assume you are referring to coding for a browser.  I suspect that you
> could code this without too much difficulty.  Maybe create an array from
> the nodeset and sort it.
>
> -rick
>
> At 01:40 PM 6/4/03 -0700, you wrote:
> >[Rick]
> >Any way to do this without a stylesheet, just with the XML Dom (with a
> >method)?
> >I'm trying to avoid the whole "transformation" piece of this... but mabye
I
> >can't.
> >Karl
> >
> >----- Original Message -----
> >From: "Rick Taylor" <taylor@xxxxxxxx>
> >To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> >Sent: Wednesday, June 04, 2003 12:34 PM
> >Subject: Re: [xsl] How to sort Using Dom and SelectNodes
> >
> >
> > > Karl,
> > >
> > > This should give you what you want.
> > >
> > >          <xsl:template match="xml">
> > >            <xsl:apply-templates select="FLD[@order]">
> > >              <xsl:sort select="@order" data-type="number"/>
> > >            </xsl:apply-templates>
> > >          </xsl:template>
> > >
> > >          <xsl:template match="FLD">
> > >             <xsl:value-of select="."/>
> > >         </xsl:template>
> > >
> > > -rick
> > >
> > > At 11:40 AM 6/4/03 -0700, you wrote:
> > > >Hello,
> > > >
> > > >I have a simple selectnodes method which grabs nodes from a nodes
list
> >who
> > > >have the attribute ORDER.  I'd actually like to select and then order
the
> > > >result based on the value of the ORDER attribute.  So:
> > > >
> > > ><xml>
> > > >     <FLD order="3">run</FLD>
> > > >     <FLD order="1">See</FLD>
> > > >     <FLD>foo</FLD>
> > > >     <FLD order="2">spot</FLD>
> > > >     <FLD>bar</FLD>
> > > ></xml>
> > > >
> > > >Would yield the result fragment (tree?):
> > > >     <FLD order="1">See</FLD>
> > > >     <FLD order="2">spot</FLD>
> > > >     <FLD order="3">run</FLD>
> > > >
> > > >How can I achieve this?
> > > >
> > > >Karl
> > > >
> > > >
> > > >  XSL-List info and archive:
http://www.mulberrytech.com/xsl/xsl-list
> > >
> > > Rick Taylor
> > > XML Developer
> > > PPDM Association
> > >
> > >
> > >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > >
> > >
> > >
> >
> >
> >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
> Rick Taylor
> XML Developer
> PPDM Association
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread