RE: merging unknown nodes

Subject: RE: merging unknown nodes
From: Kay Michael <Michael.Kay@xxxxxxx>
Date: Wed, 1 Nov 2000 18:15:41 -0400 (EST)
> What I want to be able to do is transform this:
> 
> <PRODUCTS>
>     <PRODUCT name="bill_product">
>         <OPTIONLIST name="fred"> <!-- The first 'fred' option list -->
>             <OPTION>fred1</OPTION>
>             <OPTION>fred2</OPTION>
>         </OPTIONLIST>
>         <OPTIONLIST name="george">
>             <OPTION>george1</OPTION>
>         </OPTIONLIST>
>     </PRODUCT>
> 
>     <PRODUCT name="bob_product">
>         <OPTIONLIST name="fred">   <!-- The second 'fred' 
> optionlist -->
>             <OPTION>fred3</OPTION>
>             <OPTION>fred4</OPTION>
>         </OPTIONLIST>
>         <OPTIONLIST name="george">
>             <OPTION>george2</OPTION>
>         </OPTIONLIST>
>     </PRODUCT>
> <PRODUCTS>
> 
> Into this:
> 
> <OPTIONLIST name="fred">	<!-- All the 'fred' 
> optionlist's merged into
> one -->
>     <OPTION>fred1</OPTION>
>     <OPTION>fred2</OPTION>
>     <OPTION>fred3</OPTION>
>     <OPTION>fred4</OPTION>
> </OPTIONLIST>

This is a classic grouping problem, you will find solutions on the FAQ
(follow the links in the mail footer) under sorting and grouping.

Grouping problems always involve two nested loops. The outer loop finds a
representative instance of each group:

xsl:for-each select="//option[not(.=preceding::option)]"

The inner loop selects the remaining members of the group

xsl:for-each select="//option[.=current()]" 

Using "//" and "preceding", is expensive, the above illustrates the most
general solution, it's better to use preceding-sibling when possible. There
are also more efficient approaches using keys: look for the "Muenchian
grouping method".

Mike Kay




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


Current Thread