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
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/
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/ >