Subject: RE: Is it possible to sort the way I want
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Fri, 9 Apr 2004 09:51:28 +0100
|
If there's a limit on the nesting depth you can declare a series of sort
keys:
<xsl:sort select="(ancestor::*/@name)[1]"/>
<xsl:sort select="(ancestor::*/@name)[2]"/>
<xsl:sort select="(ancestor::*/@name)[3]"/>
<xsl:sort select="(ancestor::*/@name)[4]"/>
<xsl:sort select="(ancestor::*/@name)[5]"/>
Otherwise you need a multi-phase approach: one phase to compute the sort
keys, the second phase to do the sort.
It's easy in XSLT 2.0, just do
<xsl:sort select="string-join(ancestor::*/@name, '.')"/>
Michael Kay
> -----Original Message-----
> From: Ian Lang [mailto:ianplang@xxxxxxxxx]
> Sent: 09 April 2004 05:58
> To: XSLT List
> Subject: Is it possible to sort the way I want
>
> I am building a flat table from the contents of a
> hierarchical XML tree. I want to sort on the fully
> qualified name of the element but the data does not
> have a qname attribute. Is it possible?
>
> For example the data might look like this (a
> representation of Java code perhaps):
> <package name="serviceb">
> <package name="subpackageofb">
> </pacakge>
> <package name="anothersubpackagetofb">
> </pacakge>
> </pacakge>
> <package name="servicea">
> <package name="subpackageofa">
> <package name="subsubpackageofa">
> </pacakge>
> </pacakge>
> <package name="anothersubpacakgeofa">
> </pacakge>
> </pacakge>
> <package name="servicec">
> </pacakge>
>
> and I want the flat list to look like this:
> serviceA
> serviceA.anothersubpackageofa
> serviceA.subpackageofa
> serviceA.subpackageofa.subsubpackageofa
> serviceB
> serivceB.anothersubpackageofa
> serviceB.subpackageofa
> serivceC
>
> when I process this using:
> <xsl:for-each select="//package">
> <xsl:sort select="@name"/>
> stuff to build the table...
>
> I get this as my order
> serviceA
> serviceB
> serivceC
> serivceB.anothersubpackageofa
> serviceA.anothersubpackageofa
> serviceA.subpackageofa.subsubpackageofa
> serviceB.subpackageofa
> serviceA.subpackageofa
>
> which is as expected since it is sorting on just the
> name of the package.
>
> I also tried this:
> <xsl:for-each select="//package">
> <xsl:sort select="ancestor-or-self::*/@name"/>
> but that seemed to give me document order or at least
> not what I want.
>
> Can this be done directly? Can I derive the fully
> qualified name using XPath for use in the select of
> the sort element? I have a template that outputs the
> fully qualified name but it will not 'fit' in a select
> statement. Or am I forced to insert the qualified
> name into the XML?
>
> Any advice would be appricated.
>
> Thanks,
>
> IL
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Small Business $15K Web Design Giveaway
> http://promotions.yahoo.com/design_giveaway/
|