xsl-list
[Top] [All Lists]

Re: Can grouping and sorting be done in single transformation?

2003-11-10 04:28:54
Well, here is one ugly and processor function dependant solution. 
For what it is worth:


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                   version="1.0"
                   xmlns:xalan="http://xml.apache.org/xalan";
                   exclude-result-prefixes="xalan">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <result>
<!-- create RTF to manipulate -->
    <xsl:variable name="rtf">
      <sorted>
        <xsl:for-each select="/result/item">
        <xsl:sort select="@desc"/>
        <item desc="{(_at_)desc}"/>
        </xsl:for-each>
        </sorted>
    </xsl:variable>
 
<!-- call template triples to get the groups -->
  <xsl:call-template name="triples">
    <xsl:with-param name="nodes"
select="xalan:nodeset($rtf)/sorted/item/@desc"/>
  </xsl:call-template>
 
  </result>
</xsl:template>
 
<xsl:template name="triples">
 
  <xsl:param name="nodes"/>
<group>
  <item desc="{$nodes[1]}"/>
  <item desc="{$nodes[2]}"/>
  <item desc="{$nodes[3]}"/>
</group>
  <xsl:if test="count($nodes) > 3">
    <xsl:call-template name="triples">
      <xsl:with-param name="nodes"
                      select="$nodes[position() > 3]"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
</xsl:stylesheet>




Lainaus Herman Kwok <herman(_dot_)kwok(_at_)technologist(_dot_)com>:
<!-- Hi all,
<!-- 
<!-- I am new to XSL. I am woundering if sorting and grouping can be done in
<!-- single transformation. If it is possible, would you please tell me how?
<!-- 
<!-- Here is the screnario.
<!-- 
<!-- XML Data
<!-- =========
<!-- <result>
<!--     <item desc="d"/>
<!--     <item desc="j"/>
<!--     <item desc="k"/>
<!--     <item desc="e"/>
<!--     <item desc="c"/>
<!--     <item desc="g"/>
<!--     <item desc="h"/>
<!--     <item desc="i"/>
<!--     <item desc="f"/>
<!--     <item desc="a"/>
<!--     <item desc="b"/>
<!-- </result>
<!-- 
<!-- expected result after transformation
<!-- (all the items are sortted by the value of "desc" and 
<!-- arranged x (say 3) items in a group)
<!-- =====================================
<!-- <result>
<!--   <group>
<!--     <item desc="a"/>
<!--     <item desc="b"/>
<!--     <item desc="c"/>
<!--   </group>
<!--   <group>
<!--     <item desc="d"/>
<!--     <item desc="e"/>
<!--     <item desc="f"/>
<!--   </group>
<!--   <group>
<!--     <item desc="g"/>
<!--     <item desc="h"/>
<!--     <item desc="i"/>
<!--   </group>
<!--   <group>
<!--     <item desc="j"/>
<!--     <item desc="k"/>
<!--   </group>
<!-- </result>
<!-- 
<!-- Any idea? 
<!-- 
<!-- 
<!-- -- 
<!-- __________________________________________________________
<!-- Sign-up for your own personalized E-mail at Mail.com
<!-- http://www.mail.com/?sr=signup
<!-- 
<!-- Search Smarter - get the new eXact Search Bar for free!
<!-- http://www.exactsearchbar.com/
<!-- 
<!-- 
<!--  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
<!-- 
<!-- 


***************************************************
* Jarkko Moilanen                                 *
* Project Manager, ITCM (www.itcm.org)            *
* Profound XML technology Expert                  *
* University of Tampere                           *
* Hypermedia Laboratory                           *
***************************************************

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>