Re: [xsl] Help regarding xsl:for-each-group

Subject: Re: [xsl] Help regarding xsl:for-each-group
From: "Mailing Lists Mail daktapaal@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 11 Jul 2025 04:44:12 -0000
SOmething like this?


<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  exclude-result-prefixes="w">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/w:body">
    <!-- Output non-index content first -->
    <xsl:for-each select="w:p[w:pPr/w:pStyle/@w:val='ParaFL']">
      <p class="textfl">
        <xsl:value-of select="w:r/w:t"/>
      </p>
    </xsl:for-each>

    <div>
      <xsl:for-each-group select="w:p"
group-starting-with="w:p[w:pPr/w:pStyle/@w:val='Indexentry']">
        <xsl:if test="current-group()[1]/w:pPr/w:pStyle/@w:val =
'Indexentry'">
          <idxentry>
            <xsl:attribute name="id">
              <xsl:text>idx</xsl:text>
              <xsl:number/>
            </xsl:attribute>
            <ie_f>
              <xsl:value-of select="current-group()[1]/w:r/w:t"/>
            </ie_f>

            <xsl:for-each select="current-group()[position() > 1]">
              <xsl:variable name="style" select="w:pPr/w:pStyle/@w:val"/>
              <xsl:choose>
                <xsl:when test="$style = "'Indexsubentry'">
                  <ie_s><xsl:value-of select="w:r/w:t"/></ie_s>
                </xsl:when>
                <xsl:when test="$style = "'Indexsubsubentry'">
                  <ie_t><xsl:value-of select="w:r/w:t"/></ie_t>
                </xsl:when>
              </xsl:choose>
            </xsl:for-each>

          </idxentry>
        </xsl:if>
      </xsl:for-each-group>
    </div>
  </xsl:template>

</xsl:stylesheet>

On Thu, Mar 27, 2025 at 8:34b/AM J.S. Rawat jogarawat@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> thanks Michael, I tried above code, but the result is as below"
>
> <idxentry
id="idx1"><ie_f>Paragraph</ie_f><ie_s>Paragraph</ie_s><ie_t>Paragraph
> </ie_t></idxentry>
> <idxentry
id="idx2"><ie_f>Entry1</ie_f><ie_s>Entry1</ie_s><ie_t>Entry1</ie_t></idxentry
>
> ...
>
> On Wed, Mar 26, 2025 at 10:57b/PM Michael Kay michaelkay90@xxxxxxxxx
> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > I think you want something along the lines
> >
> > <xsl:for-each-group select="*"
group-starting-with="w:p[.//@w:val='Indexentry']">
> >    <idxentry id="idx{position()}">
> >       <ie_t><xsl:value-of select=".//w:t"/></ie_t>
> >       <xsl:for-each-group select="current-group()"
group-starting-with="w:p[.//@w:val='Indexsubentry']">
> >          <ie_s>...</ie_s>
> >          <xsl:for-each-group select="current-group()"
group-starting-with="w:p[.//@w:val='Indexsubentry']">
> >              <ie_t>...</ie_t>
> >          </xsl:for-each-group>
> >       </xsl:for-each-group>
> >    </idxentry>
> > </xsl:for-each-group>
> >
> >
> > > On 26 Mar 2025, at 16:37, J.S. Rawat jogarawat@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> > >
> > > Hi All,
> > > I have generated the below output by another method in xsl version
> > > 1.0. Can we get this result by using for-each-group.
> > >
> > > INPUT
> > > <w:body>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="ParaFL"/></w:pPr><w:r><w:t>Paragraph</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexentry"/></w:pPr><w:r><w:t>Entry1</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexentry"/></w:pPr><w:r><w:t>Entry2</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexsubentry"/></w:pPr><w:r><w:t>Subentry1</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexsubentry"/></w:pPr><w:r><w:t>Subentry2</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexentry"/></w:pPr><w:r><w:t>Entry3</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexentry"/></w:pPr><w:r><w:t>Entry4</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexsubentry"/></w:pPr><w:r><w:t>Subentry3</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > >
w:val="Indexsubsubentry"/></w:pPr><w:r><w:t>Subsubentry1</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexsubentry"/></w:pPr><w:r><w:t>Subentry4</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > >
w:val="Indexsubsubentry"/></w:pPr><w:r><w:t>Subsubentry2</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > >
w:val="Indexsubsubentry"/></w:pPr><w:r><w:t>Subsubentry3</w:t></w:r></w:p>
> > > <w:p><w:pPr><w:pStyle
> > > w:val="Indexentry"/></w:pPr><w:r><w:t>Entry5</w:t></w:r></w:p>
> > > </w:body>
> > >
> > > OUTPUT
> > > <p class="textfl">Paragraph</p>
> > > <div>
> > > <idxentry id="idx1"><ie_f>Entry1</ie_f></idxentry>
> > > <idxentry
id="idx2"><ie_f>Entry2</ie_f><ie_s>Subentry1</ie_s><ie_s>Subentry2</ie_s></id
xentry>
> > > <idxentry id="idx3"><ie_f>Entry3</ie_f></idxentry>
> > > <idxentry
id="idx4"><ie_f>Entry4</ie_f><ie_s>Subentry3</ie_s><ie_t>Subsubentry1</ie_t><
ie_s>Subentry4</ie_s><ie_t>Subsubentry2</ie_t><ie_t>Subsubentry3</ie_t>
> > > <idxentry id="idx5"><ie_f>Entry5</ie_f></idxentry></idxentry>
> > > </div>
> > >
> > > thanks
> > > ...JSR

Current Thread