[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Group and sort by group element occurrences


Subject: Re: [xsl] Group and sort by group element occurrences
From: Mukul Gandhi <mukulw3@xxxxxxxxx>
Date: Mon, 18 Aug 2003 23:09:14 -0700 (PDT)

Please try the XSL below --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan">
   <xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>
   <xsl:key name="x" match="docroot/token/pageid"
use="."/>
   <xsl:template match="/">
     <xsl:variable name="treefrag">
	<docroot>
	  <xsl:for-each select="docroot/token/pageid">
	    <xsl:if test="generate-id(.) =
generate-id(key('x', .)[1])">
	      <page>
		<pageid>
		  <xsl:value-of select="."/>
		</pageid>
		<no_of_times>
		<xsl:value-of select="count(key('x', .))"/>
		</no_of_times>
	      </page>
	    </xsl:if>
	  </xsl:for-each>
	</docroot>
      </xsl:variable>
      
      <xsl:call-template name="process_tree">
	 <xsl:with-param name="tree"
select="xalan:nodeset($treefrag)"/>
      </xsl:call-template>
</xsl:template>

<xsl:template name="process_tree">
   <xsl:param name="tree"/>
   <docroot>
     <xsl:for-each select="$tree/docroot/page">
	<xsl:sort select="no_of_times" data-type="number"
order="descending"/>
	  <pageid>
	    <xsl:value-of select="pageid"/>
	  </pageid>
      </xsl:for-each>
    </docroot>
</xsl:template>
</xsl:stylesheet>

We need to use Muenchian method for grouping. I have
used xalan:nodeset extension function. Jarno's
solution looks simpler..


Regards,
Mukul

--- "Simerman, Joshua Michael" <jsimerma@xxxxxxxxxxx>
wrote:
> Long time reader, first time poster.
> 
> I have an xml doc that is the result of a keyword
> search, which list the
> keyword, and all pages by id that have that instance
> of the keyword. The
> output looks like this.
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <docroot>
> 	<token>apply
> 		<pageid>1</pageid>
> 		<pageid>3</pageid>
> 		<pageid>84</pageid>
> 	</token>
> 	<token>international
> 		<pageid>3</pageid>
> 		<pageid>5</pageid>
> 		<pageid>84</pageid>
> 	</token>
> 		<token>financial aid
> 		<pageid>5</pageid>
> 		<pageid>84</pageid>
> 	</token>
> </docroot>
> 
> I need to transform this this into a grouped and
> sorted list of page
> id's, so that the instances of pageid that occur the
> most frequently are
> first, and so that each pageid is listed only once.
> The above xml would
> then look like this.
> 
> <docroot>
> 	<pageid>84</pageid>
> 	<pageid>3</pageid>
> 	<pageid>5</pageid>
> 	<pageid>1</pageid>
> </docroot>
> 
> It seems like a fairly simple transform, but I just
> cant get it to sort
> properly and then have only the unique page ids. I
> just end up with a
> flat list of the original document, or nothing at
> all.
> 
> Thanks,
> 
> Josh Simerman
> Graduate Assistant, Web Developer
> Systems & Accounting Graduate Programs
> Indiana University, Kelley School of Business
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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



Current Thread
Keywords