I have the following sample nested XML
<?xml version="1.0" encoding="UTF-8"?>
<text>
<main>
<div n="Section 1">
<milestone unit="fragment"/>
<div rend="header">
<head>One</head>
</div>
</div>
<div n="Section 2">
<milestone unit="fragment"/>
<div rend="header">
<head>Two</head>
</div>
<div>
<p>Para 1</p>
<p>Para 2</p>
<p>Para 3</p>
<milestone unit="fragment"/>
<p>Para 4</p>
<p>Para 5</p>
<p>Para 6</p>
<milestone unit="fragment"/>
<p>Para 7</p>
<p>Para 8</p>
<p>Para 9</p>
</div>
</div>
</main>
</text>
I am trying to group it into fragments like this:
<div n="1">
One
</div>
<div n="2">
Two
Para 1
Para 2
Para 3
</div>
<div n="3">
Para 4
Para 5
Para 6
</div>
<div n="4">
Para 7
Para 8
Para 9
</div>
This is the XSLT that I am using
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text">
<xsl:for-each select="main/div">
<xsl:for-each-group select="*" group-starting-with="milestone[@unit='fragment']">
<xsl:for-each select="current-group()" >
<xsl:variable name="fragNum"><xsl:number count="milestone[@unit='fragment']" level="any" from="body"/></xsl:variable>
<div n="{$fragNum}">
<xsl:apply-templates select="."/>
</div>
</xsl:for-each>
</xsl:for-each-group>
</xsl:for-each>
</xsl:template>
This is the output of the transform:
<div n=""/>
<div n="">
One
</div>
<div n=""/>
<div n="">
Two
</div>
<div n="">
Para 1
Para 2
Para 3
Para 4
Para 5
Para 6
Para 7
Para 8
Para 9
</div>
Even though I am telling for-each-group that the grouping should start with "milestone[@unit='fragment'] why is it ignoring it when the milestone tag is nested inside a div?