Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Advanced sort >Thread Next - Re: Advanced sort Re: Advanced sortTo: NULL Date: 3/15/2009 12:15:00 PM Delf wrote:
> I think this is a solution:
>
> <xsl:key name="id" match="tag" use="." />
>
> <xsl:variable name="groupTagList">
> <tags>
>
> <xsl:for-each select="//data/items/item[ @isVisible = '1' ]
> /tags/tag[ generate-id( . ) = generate-id( key( 'id', . )[1] )]">
>
> <tag>
> <xsl:attribute name="number">
> <xsl:value-of select="count( //data/items/
> item[ @isVisible = '1' ]/tags/tag[ . = current() ] )" />
>
> </xsl:attribute>
> <xsl:value-of select="." />
> </tag>
> </xsl:for-each>
> </tags>
> </xsl:variable>
>
> <xsl:variable name="tagList">
> <tags>
> <xsl:apply-templates select="msxsl:node-set(
> $groupTagList )//tags/tag" mode="sorting">
>
> <xsl:sort select="@number" data-type="number" order="descending" />
> <xsl:sort select="." data-type="text" order="ascending" />
> </xsl:apply-templates>
> </tags>
> </xsl:variable>
>
> <xsl:template match="tag" mode="sorting">
> <xsl:copy-of select="." />
> </xsl:template>
>
> And I get the following output:
>
> achat conpulsif (2)
> art (2)
> peinture (2)
> musique (1)
> piano (1)
>
> Order by DESC number and then ASC label.
That data above is not what you posted initially.
Your stylesheet has the right approach with the key but I think it can
be much shorter, at least for the data you posted originally:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="by-tag"
match="tag"
use="."/>
<xsl:template match="/items">
<tags>
<xsl:apply-templates select="item/tags/tag[generate-id() =
generate-id(key('by-tag', .)[1])]">
<xsl:sort select="count(key('by-tag', .))" data-type="number"
order="descending"/>
</xsl:apply-templates>
</tags>
</xsl:template>
<xsl:template match="tag">
<tag id="{.}">
<xsl:value-of select="count(key('by-tag', .))"/>
</tag>
</xsl:template>
</xsl:stylesheet>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
