Re: [xsl] Muenchian grouping not working in my case - what am I doing wrong

Subject: Re: [xsl] Muenchian grouping not working in my case - what am I doing wrong
From: Mukul Gandhi <mukulgandhi2003@xxxxxxxxxxx>
Date: Sat, 24 Apr 2004 07:22:13 +0100 (BST)
Hi Ian,

The following XSL might be useful.  It applies
Muenchian grouping to subsets of XML retrieved using
xsl:copy-of, which are stored in a variable.  A
"nodeset" function is then used to process these
subsets.

<?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="html" version="1.0"
encoding="UTF-8" indent="yes"/>
<xsl:key name="group-by-category" match="property"
use="@category"/>
<xsl:template match="/document">
   <xsl:for-each select=".//properties">
       <xsl:variable name="rtf">
	  <xsl:copy-of select="."/>
       </xsl:variable>

       <xsl:for-each
select="xalan:nodeset($rtf)/properties/property">
	  <xsl:if test="generate-id(.) =
generate-id(key('group-by-category', @category)[1])">
	     <xsl:value-of select="@category"/><br/>
	     <xsl:for-each select="key('group-by-category',
@category)">
		<xsl:text>Property</xsl:text>
		<xsl:value-of select="@name"/>
		<xsl:value-of select="@value"/>
		<br/>
	     </xsl:for-each>
	  </xsl:if>
       </xsl:for-each>

    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Regards,
Mukul

 --- Ian Lang <ianplang@xxxxxxxxx> wrote: > I have a
hierarchical document where each of the
> child
> or container elements can have a properties element
> which contains n property elements.  Each of the
> property elements has a name, a value and a
> category. 
> I am generating HTML for the document and for each
> element I want to create a property table which
> shows
> the properties by category.  For some reason
> grouping
> using the Muenchian method does not work as I
> expected
> but grouping the slow way (as found in the FAQ) does
> work.  I strongly suspect that I am missing
> something
> important but I cannot figure out what it is.
> 
> Here is a sample document (namespaces have been
> removed for readability):
> <document>
>   <container>
>     <properties>
>       <property name=test category=Cat1
> value=valueA/>
>       <property name=testing category=Cat1
> value=valueB/>
>       <property name=test category=Cat2
> value=valueC/>
>       <property name=testing category=Cat2
> value=valueD/>
>     </properties>
>     <container>
>       <child>
>         <properties>
>           <property name=test category=Cat1
> value=valueA/>
>           <property name=testing category=Cat1
> value=valueB/>
>           <property name=test category=Cat2
> value=valueC/>
>           <property name=testing category=Cat2
> value=valueD/>
>         </properties>
>       </child>
>     </container
>     <child>
>       <properties>
>         <property name=test category=Cat1
> value=valueA/>
>         <property name=testing category=Cat1
> value=valueB/>
>         <property name=test category=Cat2
> value=valueC/>
>         <property name=testing category=Cat2
> value=valueD/>
>       </properties>
>     </child>
>   </container>
> </document>
> 
> When creating the HTML for each of the container or
> child nodes I include this line at the location
> where
> I would like the property table:
> <xsl:apply-templates select="properties"/>
> 
> To create the table I have this key and template for
> the properties object.  Right now I have it dump the
> properties with no grouping, with Muenchian grouping
> and then with the slow method of grouping as a test:
> <xsl:key name="properties-by-category"
> match="/*/property" use="@category"/>
>   
> <xsl:template match="properties">
>   <h4>Properties raw</h4>
>   <xsl:for-each select=" property">
>     <xsl:value-of select="@ category"/><br/>
>             <xsl:text>Property
> </xsl:text><xsl:value-of
> select="@name"/> <xsl:value-of
> select="@value"/><br/>
>   </xsl:for-each>
> 
>   <h4>Properties fast grouping</h4>
>   <xsl:for-each select="property[count(. |
> key('properties-by-category', @category)[1]) = 1]">
>     <xsl:sort select="@category"/>
>     <xsl:value-of select="@category"/><br/>
>     <xsl:for-each
> select="key('properties-by-category', @category)">
>       <xsl:sort select="@name"/>
>             <xsl:text>Property
> </xsl:text><xsl:value-of
> select="@name"/> <xsl:value-of
> select="@value"/><br/>
>     </xsl:for-each>
>   </xsl:for-each>
> 
>   <h4>Properties slow grouping</h4>
>   <xsl:for-each
>
select="property[not(@category=preceding-sibling::property/@category)]">
>     <xsl:sort select="@category"/>
>     <xsl:value-of select="@category"/><br/>
>     <xsl:for-each
>
select=".|following-sibling::property[@category=current()/@category]">
>       <xsl:sort select="@name"/>
>             <xsl:text>Property
> </xsl:text><xsl:value-of
> select="@name"/> <xsl:value-of
> select="@value"/><br/>
>     </xsl:for-each>
>   </xsl:for-each>
>     
> </xsl:template>
> 
> For each of the property tables I get something like
> this:
> Properties raw
> 
> Cat1
>     Property test valueA
> Cat1
>     Property testing valueB
> Cat2
>     Property test valueC
> Cat2
>     Property testing valueD
> 
> Properties fast grouping
> Cat1
> Cat1
> Cat2
> Cat2
> 
> Properties slow grouping
> Cat1
>     Property test valueA
>     Property testing valueB
> Cat2
>     Property test valueC
>     Property testing valueD
> 
> As you can see the values I want are there but my
> Muenchain grouping is not working as expected but
> the
> slow method is.
> 
> Is Muenchian grouping inappropriate for my situation
> or did I mess up.  Any advice would be appreciated
> 
> Thanks,
> 
> IL
> 
> 
> 	
> 		
> __________________________________
> Do you Yahoo!?
> Yahoo! Photos: High-quality 4x6 digital prints for
> 25"
> http://photos.yahoo.com/ph/print_splash
>  

________________________________________________________________________
Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/

Current Thread