Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Advanced sort

From: Martin Honnen <mahotrash@-----.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent