## RE: [xsl] Calculating groups of repeating elements

 Subject: RE: [xsl] Calculating groups of repeating elements From: "Michael Kay" Date: Thu, 11 Dec 2008 20:11:10 -0000
```> Now finding the 1-groups looks like this:
>
> <xsl:function name="f:level-one-groups" as="element(group)*">
> <for-each-group select="//word" group-by=".">
>   <xsl:if test="count(current-group()) gt 1">
>     <group>
>       <word><xsl:value-of select="current-grouping-key()"/></word>
>       <xsl:copy-of select="place_number"/>
>     </group>
>   </xsl:if>
> </xsl:for-each-group>
> </xsl:function>
>
> and finding the level-n groups given the level-n-1-groups is:
>
> <xsl:function name="f:level-n-groups" as="element(group)*">
>   <xsl:param name="level-n-1-groups" as="element(group)*"/>
>       <xsl:variable name="g" select="\$level-n-1-groups"/>
>       <xsl:for-each select="\$g">
>         <xsl:variable name="places"
>                       select="//place[place-number =
> \$g/place_number]"/>
>         <xsl:variable name="otherWords"
>                       select="\$places/words/word[not(. = \$g/word)]"/>

There needs to be an <xsl:for-each select="\$otherWords"> in here. Sorry! -
MK

>         <xsl:variable name="n-gram"
>                       select="\$g/word, ."/>
>         <xsl:variable name="places-with-n-gram"
>                       select="\$places[every \$w in n-gram
> satisfies ./words/word = \$w]"/>
>         <xsl:if test="count(\$places-with-n-gram) gt 1">
>           <group>
>             <xsl:for-each select="\$n-gram">
>                <word><xsl:value-of select="."/>
>             </xsl:for-each>
>             <xsl:for-each select="\$places-with-n-gram">
>                <word><xsl:value-of select="."/>
>             </xsl:for-each>
>           </group>
>         </xsl:if>
>      </xsl:for-each>
> </xsl:function>
>
> then you just have to do a recursion in which you start by finding the
> level-1 groups, then recurse to levels 2, 3, 4 etc until you
> find there are no groups at a particular level.
>
> Not tested, of course, and almost certainly capable of improvement.
>
> Michael Kay
> http://www.saxonica.com/

```