Re: [xsl] creating multi-level TOC from existing list

Subject: Re: [xsl] creating multi-level TOC from existing list
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 5 Jun 2020 05:45:44 -0000
Am 05.06.2020 um 01:18 schrieb Charlie0 charlieo0@xxxxxxxxxxx:
I need help with XSLT 2.0. I am completely stumped on how to proceed. I
have tried doing string comparisons and that is not working. I am sure
there is way to use grouping to accomplish what I need, but I just don't
know how to create two separate groups and build the TOC I need. I have
no code to show. Any help to put me in the right direction is appreciated.

Below is the input, followed by the desired output. Text has been
distorted due company policies.

Input snippet:

<toc>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0100 VNGKNV XSSVMBLY">
 B B B B B B B B B B B  <tocEntry filename="M2390992350401.xml"
title="RVZXKR LUBV
CKL FKLTVR XSSVMBLY"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0100 VNGKNV XSSVMBLY">
 B B B B B B B B B B B  <tocEntry filename="M2812292350401.xml"
title="RVZLXCV
HYDRXULKC RVSVRVCKR BRXCKVT"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0100 VNGKNV XSSVMBLY">
 B B B B B B B B B B B  <tocEntry filename="M3007592350401.xml"
 B B B B B B B B B B B B B B B  title="RVZLXCV VNGKNV FRCM SHKZZKNG/STCRXGV
CCNTXKNVR"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0102 CRXNKSHXFT">
 B B B B B B B B B B B  <tocEntry filename="M2356292350401.xml"
 B B B B B B B B B B B B B B B  title="RVZLXCV VKBRXTKCN DXMZVR XND
CRXNKSHXFT ZULLVY"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0102 CRXNKSHXFT">
 B B B B B B B B B B B  <tocEntry filename="M2357392350401.xml"
title="RVZLXCV RVXR
CRXNKSHXFT SVXL"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0103 FLYWHVVL XSSVMBLY">
 B B B B B B B B B B B  <tocEntry filename="M2357292350401.xml"
title="RVZLXCV
FLYWHVVL"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="01 VNGKNV">
 B B B B B B B  <tocEntry title="0103 FLYWHVVL XSSVMBLY">
 B B B B B B B B B B B  <tocEntry filename="M2390892350401.xml"
title="RVZLXCV RVXR
CRXNKSHXFT XDXZTVR"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="03 FUVL SYSTVM">
 B B B B B B B  <tocEntry title="0304 XKR CLVXNVR">
 B B B B B B B B B B B  <tocEntry filename="M2142592350401.xml"
title="RVZLXCV XKR
CLVXNVR BCCT"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="03 FUVL SYSTVM">
 B B B B B B B  <tocEntry title="0304 XKR CLVXNVR">
 B B B B B B B B B B B  <tocEntry filename="M2376292350401.xml"
 B B B B B B B B B B B B B B B  title="RVZLXCV TURBCCHXRGVR CCMZRVSSCR
KNTXKV
ZRVSSURV/TVMZVRXTURV SVNSCR"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="03 FUVL SYSTVM">
 B B B B B B B  <tocEntry title="0305 SUZVRCHXRGVR, BLCWVR, TURBCCHXRGVR CR
XLTKTUDV CCMZVNSXTCR">
 B B B B B B B B B B B  <tocEntry filename="M2249792350401.xml"
title="RVZLXCV
TURBCCHXRGVR "/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
</toc>

Desired output:


<toc> B B B <tocEntry title="01 VNGKNV"> B B B B B B B <tocEntry title="0100 VNGKNV XSSVMBLY"> B B B B B B B B B B B <tocEntry filename="M2390992350401.xml"
title="RVZXKR LUBV
CKL FKLTVR XSSVMBLY"/>
 B B B B B B B  <tocEntry filename="M2812292350401.xml" title="RVZLXCV
HYDRXULKC RVSVRVCKR BRXCKVT"/>
 B B B B B B B B B B B  <tocEntry filename="M3007592350401.xml"
 B B B B B B B B B B B B B B B  title="RVZLXCV VNGKNV FRCM SHKZZKNG/STCRXGV
CCNTXKNVR"/></tocEntry>
 B B B B B B B  <tocEntry title="0102 CRXNKSHXFT">
 B B B B B B B B B B B  <tocEntry filename="M2356292350401.xml"
title="RVZLXCV
VKBRXTKCN DXMZVR XND CRXNKSHXFT ZULLVY"/>
 B B B B B B B B B B B  <tocEntry filename="M2357392350401.xml"
title="RVZLXCV RVXR
CRXNKSHXFT SVXL"/>
 B B B B B B B  </tocEntry>
 B B B B B B B  <tocEntry title="0103 FLYWHVVL XSSVMBLY">
 B B B B B B B B B B B  <tocEntry filename="M2357292350401.xml"
title="RVZLXCV
FLYWHVVL"/>
 B B B B B B B B B B B  <tocEntry filename="M2390892350401.xml"
title="RVZLXCV
CRXNKSHXFT XDXZTVR"/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
 B B B  <tocEntry title="03 FUVL SYSTVM">
 B B B B B B B  <tocEntry title="0304 XKR CLVXNVR">
 B B B B B B B B B B B  <tocEntry filename="M2142592350401.xml"
title="RVZLXCV XKR
CLVXNVR BCCT"/>
 B B B B B B B B B B B  <tocEntry filename="M2376292350401.xml"
 B B B B B B B B B B B B B B B  title="RVZLXCV TURBCCHXRGVR CCMZRVSSCR
KNTXKV
ZRVSSURV/TVMZVRXTURV SVNSCR"/>
 B B B B B B B  </tocEntry>
 B B B B B B B  <tocEntry title="0305 SUZVRCHXRGVR, BLCWVR, TURBCCHXRGVR CR
XLTKTUDV CCMZVNSXTCR">
 B B B B B B B B B B B  <tocEntry filename="M2249792350401.xml"
title="RVZLXCV
TURBCCHXRGVR "/>
 B B B B B B B  </tocEntry>
 B B B  </tocEntry>
</toc>


For two levels you can just nest two for-each-group

  <xsl:template match="toc">
      <xsl:copy>
          <xsl:for-each-group select="tocEntry" group-by="@title">
              <xsl:copy>
                  <xsl:apply-templates select="@*"/>
                  <xsl:for-each-group select="current-group()/tocEntry"
group-by="@title">
                      <xsl:copy>
                          <xsl:apply-templates select="@*,
current-group()/tocEntry"/>
                      </xsl:copy>
                  </xsl:for-each-group>
              </xsl:copy>
          </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>

for arbitrary levels you would need to morph that into a recursive
function or template.

Current Thread