Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Tue, 4 Mar 2008 11:36:00 -0800,    group: microsoft.public.xsl        back       


How to sort different nodes by attribute   
I store my database settings in xml as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Columns>
  <F1 Seq="0" />
  <F2 Seq="1" />
  <F3 Seq="3" />
  <F4 Seq="9" />
  <F5 Seq="2" />
  <F6 Seq="4" />
  <F7 Seq="8" />
  <F8 Seq="5" />
  <F9 Seq="7" />
  <F10 Seq="6" />
</Columns>

I'd like to have the user sort the column order, so the resulting xml will 
look like:

<?xml version="1.0" encoding="utf-8" ?>
<Columns>
  <F1 Seq="0" />
  <F2 Seq="1" />
  <F5 Seq="2" />
  <F3 Seq="3" />
  <F6 Seq="4" />
  <F8 Seq="5" />
  <F10 Seq="6" />
  <F9 Seq="7" />
  <F7 Seq="8" />
  <F4 Seq="9" />
</Columns>

I use the following xslt, however it didn't give the result I expect:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Columns/*">
    <xsl:copy>
      <xsl:apply-templates>
        <xsl:sort select="@Seq" order="descending" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

I'm new to xslt, I'd appreciate if anyone could show me how to create the 
xslt to address this problem.

Thanks,
P_Prdn
date: Tue, 4 Mar 2008 11:36:00 -0800   author:   P_Prdn

Re: How to sort different nodes by attribute   
P_Prdn wrote:

> I use the following xslt, however it didn't give the result I expect:
> 
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>   <xsl:template match="/Columns/*">
>     <xsl:copy>
>       <xsl:apply-templates>
>         <xsl:sort select="@Seq" order="descending" data-type="number"/>
>       </xsl:apply-templates>
>     </xsl:copy>
>   </xsl:template>
> </xsl:stylesheet>

You need to process the child elements of Columns in sorted order like this:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

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

   <xsl:strip-space elements="*"/>

   <xsl:template match="Columns">
     <xsl:copy>
       <xsl:apply-templates select="*">
         <xsl:sort select="@Seq" data-type="number"/>
       </xsl:apply-templates>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
date: Wed, 05 Mar 2008 13:36:23 +0100   author:   Martin Honnen

Re: How to sort different nodes by attribute   
I tried and it worked. Thanks for your help, Martin!

P_Prdn

"Martin Honnen" wrote:

> P_Prdn wrote:
> 
> > I use the following xslt, however it didn't give the result I expect:
> > 
> > <xsl:stylesheet version="1.0" 
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> >   <xsl:template match="/Columns/*">
> >     <xsl:copy>
> >       <xsl:apply-templates>
> >         <xsl:sort select="@Seq" order="descending" data-type="number"/>
> >       </xsl:apply-templates>
> >     </xsl:copy>
> >   </xsl:template>
> > </xsl:stylesheet>
> 
> You need to process the child elements of Columns in sorted order like this:
> 
> <xsl:stylesheet
>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>    version="1.0">
> 
>    <xsl:output method="xml" indent="yes"/>
> 
>    <xsl:strip-space elements="*"/>
> 
>    <xsl:template match="Columns">
>      <xsl:copy>
>        <xsl:apply-templates select="*">
>          <xsl:sort select="@Seq" data-type="number"/>
>        </xsl:apply-templates>
>      </xsl:copy>
>    </xsl:template>
> 
>    <xsl:template match="@* | node()">
>      <xsl:copy>
>        <xsl:apply-templates select="@* | node()"/>
>      </xsl:copy>
>    </xsl:template>
> 
> </xsl:stylesheet>
> 
> -- 
> 
> 	Martin Honnen --- MVP XML
> 	http://JavaScript.FAQTs.com/
>
date: Wed, 5 Mar 2008 09:39:01 -0800   author:   P_Prdn

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us