开发者

Nested grouping using for-each-group

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>
           开发者_JAVA百科     <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?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜