0

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?

1
  • could be because the milestone is an empty element? i dont know xslt 2 well but im sure that this is simple enough with xslt 1
    – Treemonkey
    Feb 17, 2011 at 10:22

2 Answers 2

2

With the stylesheet being

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:output indent="yes"/>

  <xsl:template match="text">
    <xsl:for-each-group select="descendant::*[not(*)]"
      group-starting-with="milestone[@unit = 'fragment']">
      <div n="{position()}">
        <xsl:value-of select="current-group()" separator="&#10;"/>
      </div>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

Saxon 9 outputs

<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>
1
  • +1 Very good answer. Besides that it isn't needed for this case, I would strip the mark like current-group() except . or current-group()[position() ne 1]
    – user357812
    Feb 17, 2011 at 14:55
0

It looks like you are selecting only the child elements of main/div in your for-each-group. Therefore, (from the point of view of that for-each-group element) there are no milestone elements nested within the next layer of div.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.