RE: [xsl] grouping nesting items, including following items

Subject: RE: [xsl] grouping nesting items, including following items
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 19 Jun 2007 09:30:11 +0100
A tricky one, it has come up before but I don't think it will be easy to
find the thread. On that occasion I think the break was indicated by a
<newpage/> element that could appear at any depth, but the principle is the

I think the basic approach is this. When processing a <point> that contains
a descendant <newpage/>, you need to split it into two <point> elements, the
first containing everything before the <newpage/>, the second containing
everything after. And it also applies recursively, of course, to the
children. So it's something like this:

<xsl:template match="point">
  <xsl:copy-of select="."/>

<xsl:template match="point[descendant::newpage]">
  <xsl:variable name="this" select="."/>
  <xsl:variable name="np" select="descendant::newpage[1]"/>
  <point id="{@id}" part="1">
    <xsl:apply-templates select="child::node()[. << $np or (. intersect
  <xsl:variable name="remainder as="element(point)">
    <point id="{@id}" part="{1+position()}">
      <xsl:copy-of select="$this/child::node()[. >> $np or (. intersect
  <xsl:apply-templates select="$remainder"/>

This basically creates a structure in which the <newpage/> elements are
propogated to the top level, and every ancestor of a <newpage/> element is
duplicated either side of this element, but with unsplit <point> elements
appearing one side or the other. You've then got a simpler second stage of
processing in which the pages are identified at the top level (quite
possibly you can do this in the same pass but I think it's useful to get the
basic splitting logic right first).

I've used some 2.0 constructs here (and for clarity I've used the << and >>
operators without escaping) but they can probably all be translated into 1.0
equivalents with a bit of ingenuity and verbosity.

You can probably substitute point[newSlide='true'] for my <newpage/>
elements, it will add a little bit of extra complexity. I'd suggest getting
this working on a simplified version of the problem first and when you've
got something that works, extend it to handle the real problem.

This is untested and almost certainly contains bugs, but I hope the general
idea comes across.

Michael Kay 

> -----Original Message-----
> From: Don Smith [mailto:dtsmithisogen@xxxxxxxxx] 
> Sent: 19 June 2007 08:48
> To: XSLT
> Subject: [xsl] grouping nesting items, including following items
> I can't quite figure out how to group nested items and also 
> pick up items on the following axis for a given group. Here's 
> a sample source:
> <slide title="Introduction" id="x1">
>  <point id="x2" >
>   <text>First point</text>
>   <subpoints id="x3">
>    <point id="x3a">
>     <text>First point, subpoint 1</text>
>    </point>
>    <point id="x3b">
>     <text>First point, subpoint 2</text>
>    </point>
>    <point id="x3c">
>     <text>First point, subpoint 3</text>
>     <subpoints id="x3c1">
>      <point id="x3c1a" newSlide="true"  title="Intro (cont.)">
>       <text>First point, subpoint 3, sub-subpoint 1 on new 
> slide</text>
>      </point>
>      <point id="x3c1b">
>       <text>First point 4, subpoint 3, sub-subpoint 2 on new 
> slide</text>
>      </point>   
>     </subpoints>
>    </point>
>    <point id="x4d">
>     <text>First point, subpoint 4 on new slide</text>
>    </point>
>   </subpoints>
>  </point>
>  <point id="x5" >
>   <text>Second point, on new slide</text>  </point> </slide>
> Note the attribute "newSlide" on <point id="x3c1a">: I need 
> this element, all its descendants (if any), and everything 
> that follows, to be in a different group than everything that 
> comes before. Ideally, this would mean each group is placed 
> in its own document using <xsl:result-document>.
> This is just one example, but "newSlide='true'" can occur on 
> any <point> or even any <subpoints>.
> Thanks,
> Don
> ______________________________________________________________
> ______________________
> Finding fabulous fares is fun.  
> Let Yahoo! FareChase search your favorite travel sites to 
> find flight and hotel bargains.

Current Thread