Re: [xsl] XSLT 1.0: Grouping Adjacent Elements in Embedded Lists

Subject: Re: [xsl] XSLT 1.0: Grouping Adjacent Elements in Embedded Lists
From: Geert Josten <Geert.Josten@xxxxxxxxxxx>
Date: Sat, 06 Nov 2004 11:22:47 +0100
Hi Joe,

Ah! I see, my mistake. Didn't look close enough to the desired output. But it should be easy to fix using the high decomposition in groups that I applied. It makes things easier to address.

First thing that is wrong in my script is the start of a top level list. It shouldn't start it at each OL_LI as I thought, but only at the first of a chain of OL_LI and EM_OL_LI. Add a condition to the template that jumps to the start-top-level-list group and add a second suppress template for the remaining OL_LI elements to fix this:

  <xsl:template match="OL_LI">
    <!-- suppress at this level, you are outside the top-level wrappers! -->
  </xsl:template>

<xsl:template match="OL_LI[preseding-sibling::*[1][not(self::OL_LI or self::EM_OL_LI)] or not(preceding-sibling::*)]">
<!-- jump to start-top-level-list group to start top level nesting -->
<xsl:apply-templates select="." mode="start-top-level-list"/>
</xsl:template>


  <xsl:template match="EM_OL_LI">
    <!-- suppress at this level, you are outside the top-level wrappers! -->
    <!-- or isn't it? -->
    <xsl:if test="not(preceding-sibling::*[1][self::OL_LI or self::EM_OL_LI])">
      <xsl:message terminate="yes">Dangling EM_OL_LI, should not occur!</xsl:message>
    </xsl:if>
  </xsl:template>

(I added a test for no preceding siblings as well, just to be sure. And a test whether EM_OL_LI is either preceded by another EM_OL_LI or a OL_LI..)

However, now it will only handle the first part of the chain and stop at the second OL_LI. We have to fix the continue-top-level-list group as well.

Here also, there is a difference between the first of the (sub) chain and following ones. Only the first of EM_OL_LI chain should jump to start sub level list. The others should be suppressed, but still continue as to be able to reach OL_LI elements that follow EM_OL_LI items directly.

So we change the EM_OL_LI rule in the continue-top-level-list group into:

  <xsl:template match="EM_OL_LI" mode="continue-top-level-list">
    <!-- suppress at this level, you are outside the sub-level wrappers! -->
    <!-- but still continue as a next OL_LI might occur after the EM_OL_LI chain -->
    <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
  </xsl:template>

  <xsl:template match="EM_OL_LI[preceding-sibling::*[1][self::OL_LI]]" mode="continue-top-level-list">
    <!-- jump to sub-level-list group to start sub level nesting -->
    <xsl:apply-templates select="." mode="start-sub-level-list"/>
    <!-- but still continue as a next OL_LI might occur after the EM_OL_LI chain -->
    <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
  </xsl:template>

All that remains to do is to handle OL_LI elements in continue-top-level-list group:

<xsl:template match="OL_LI[preceding-sibling::*[1][self::OL_LI or self::EM_OL_LI]]" mode="continue-top-level-list">
<!-- continue this list as the chain of OL_LI and EM_OL_LI elements continues! -->
<li><xsl:apply-templates /></li>
<xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
</xsl:template>


(The condition on the last template might not be necessary, but it doesn't hurt to leave it there.)

This should do the trick for you, 'all' you have to do is to repeat this trick for the PARA and HEAD level... :-P

I expect you will agree that it would be easier if one could generate such code from a more abstract formalism...

Anyhow, together this results in the script I inserted below.

Cheers,
Geert

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<!--+ ==============================================================
    | output
    +-->

<xsl:output method="xml" indent="yes" encoding="utf-8" />

<!--+ ==============================================================
    | main group
    +-->

  <xsl:template match="DATA">
    <html>
      <xsl:apply-templates select="node()" />
    </html>
  </xsl:template>

  <xsl:template match="OL_LI">
    <!-- suppress at this level, you are outside the top-level wrappers! -->
  </xsl:template>

<xsl:template match="OL_LI[preceding-sibling::*[1][not(self::OL_LI or self::EM_OL_LI)] or not(preceding-sibling::*)]">
<!-- jump to start-top-level-list group to start top level nesting -->
<xsl:apply-templates select="." mode="start-top-level-list"/>
</xsl:template>


  <xsl:template match="EM_OL_LI">
    <!-- suppress at this level, you are outside the top-level wrappers! -->
  </xsl:template>

<!--+ ==============================================================
    | group start-top-level-list
    +-->

  <xsl:template match="@*|node()" mode="start-top-level-list">
    <!-- jump back to main group, if nothing else matches! -->
    <xsl:apply-templates select="." />
  </xsl:template>

  <xsl:template match="OL_LI" mode="start-top-level-list">
    <p>
       <ol>
          <li><xsl:apply-templates /></li>
          <!-- jump to continue-top-level-list to find out whether sub level list should be started -->
          <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
       </ol>
    </p>
  </xsl:template>

  <xsl:template match="EM_OL_LI" mode="start-top-level-list">
    <!-- suppress at this level, you are outside the top-level wrappers! -->
  </xsl:template>

<!--+ ==============================================================
    | group continue-top-level-list
    +-->

<xsl:template match="@*|node()" mode="continue-top-level-list">
<!-- skip attributes, text, comments and pi's -->
<!-- Note: you might want to copy these with xsl:copy-of select="." or call an other template group, perhaps the main group -->
<xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
</xsl:template>


  <xsl:template match="*" mode="continue-top-level-list">
    <!-- end continuation as an element is encountered that doesn't belong in this group -->
  </xsl:template>

  <xsl:template match="EM_OL_LI" mode="continue-top-level-list">
    <!-- suppress at this level, you are outside the sub-level wrappers! -->
    <!-- but still continue as a next OL_LI might occur after the EM_OL_LI chain -->
    <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
  </xsl:template>

  <xsl:template match="EM_OL_LI[preceding-sibling::*[1][self::OL_LI]]" mode="continue-top-level-list">
    <!-- jump to sub-level-list group to start sub level nesting -->
    <xsl:apply-templates select="." mode="start-sub-level-list"/>
    <!-- but still continue as a next OL_LI might occur after the EM_OL_LI chain -->
    <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
  </xsl:template>

<xsl:template match="OL_LI[preceding-sibling::*[1][self::OL_LI or self::EM_OL_LI]]" mode="continue-top-level-list">
<!-- continue this list as the chain of OL_LI and EM_OL_LI elements continues! -->
<li><xsl:apply-templates /></li>
<xsl:apply-templates select="following-sibling::node()[1]" mode="continue-top-level-list"/>
</xsl:template>


<!--+ ==============================================================
    | group start-sub-level-list
    +-->

  <xsl:template match="@*|node()" mode="start-sub-level-list">
    <!-- jump back to continue-top-level-list, if nothing else matches! -->
    <xsl:apply-templates select="." mode="continue-top-level-list" />
  </xsl:template>

  <xsl:template match="EM_OL_LI" mode="start-sub-level-list">
    <p>
       <ol>
          <li><xsl:apply-templates /></li>
          <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-sub-level-list"/>
       </ol>
    </p>
  </xsl:template>

<!--+ ==============================================================
    | group continue-sub-level-list
    +-->

<xsl:template match="@*|node()" mode="continue-sub-level-list">
<!-- skip attributes, text, comments and pi's -->
<!-- Note: you might want to copy these with xsl:copy-of select="." or call an other template group, perhaps the main group -->
<xsl:apply-templates select="following-sibling::node()[1]" mode="continue-sub-level-list"/>
</xsl:template>


  <xsl:template match="*" mode="continue-sub-level-list">
    <!-- end continuation as an element is encountered that doesn't belong in this group -->
  </xsl:template>

  <xsl:template match="EM_OL_LI" mode="continue-sub-level-list">
    <!-- output sub level list item -->
    <li><xsl:apply-templates /></li>
    <!-- continue with following EM_OM_LI elements -->
    <xsl:apply-templates select="following-sibling::node()[1]" mode="continue-sub-level-list"/>
  </xsl:template>

</xsl:stylesheet>

Current Thread