This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: apples into baskets


> What I am trying to do is to put fruits into baskets:
> 
> INPUT:
> 
> <basket/>
> <apples>3</apples>
> <oranges>5</oranges>
> <basket/>
> <apples>7</apples>
> <oranges>9</oranges>
> 

This falls into the broad category of grouping problems: see Dave Pawson's
FAQ. The most general solutions involve using a recursive named template to
process the list on element at a time. In this case I suspect something
along the following lines might work:

<xsl:template match="basket">
<basket>
   <xsl:apply-templates 
      select="following-sibling::*[
                 generate-id(preceding-sibling::basket[1])=
                 generate-id(current())]"/>
</basket>
</xsl:template>

In other words, for each <basket> element, process those of its
following-siblings that have that <basket> as their most recent <basket>
preceding-sibling.

It is of course horribly inefficient (order n squared) for a long list. So a
Muenchian solution using keys would be better:

<xsl:key name="bkey"
         match="apples|oranges|bananas" 
         use="generate-id(preceding-sibling::basket[1])"/>

<xsl:template match="basket">
  <xsl:apply-templates select="key('bkey', generate-id())"/>
</xsl:template>

Mike Kay


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]