[xsl] Flat bullets to nested ul

Subject: [xsl] Flat bullets to nested ul
From: Emma Burrows <Emma.Burrows@xxxxxxxxxxx>
Date: Fri, 1 Jul 2011 16:40:07 +0100
I am using XSLT 2.0 with Oxygen 12 and Saxon EE. I am currently working on a
document that features nested bulleted lists with sub-paragraphs, but
expressed as a flat list of paragraph elements. I am converting this to a
DITA-compliant structure. I have had some success getting the first level
working, and the bulleted paragraphs on the second level, but I'm just lacking
the extra bit to get sub-paragraphs included in the second level <li>.

But let me illustrate the problem better.

Here is my input:

<wrapper>
  <para style="PLAIN">Normal para not involved in the bullets at all.</para>
  <para style="BULLET">bullet level 1</para>
  <para style="INDENT">para involved in level 1</para>
  <para style="BULLINDENT">bullet level 2</para>
  <para style="INDENT2">mixed content</para>
  <para style="INDENT2">mixed content</para>
  <para style="PLAIN">The list is finished</para>
</wrapper>

Here is the desired output:

<p>Normal para not involved in the bullets at all.</para>
<ul>
  <li>bullet level 1
    <p>para involved in level 1</para>
   <ul>
      <li>bullet level 2
         <p>mixed content</p>
         <p>mixed content</p>
      </li>
   </ul>
  </li>
</ul>
<p>The list is finished</para>

But currently, I get:

<p>Normal para not involved in the bullets at all.</para>
<ul>
  <li>bullet level 1
    <p>para involved in level 1</para>
   <ul>
      <li>bullet level 2</li>
   </ul>
  </li>
</ul>
<p>mixed content</p>
<p>mixed content</p>
<p>The list is finished</para>

So I'm nearly there but not quite. The relevant parts of my template are as
follows:

  <xsl:template match="wrapper">
    <xsl:for-each-group select="node()"
      group-adjacent="boolean(self::para[@style='BULLET']|self::para[@style='
INDENT']|self::para[@style='BULLINDENT']|para[@style='INDENT2'])">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <xsl:call-template name="makeList">
            <xsl:with-param name="paralist" select="current-group()"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:template>

  <xsl:template name="makeList">
    <xsl:param name="paralist"/>
    <xsl:variable name="firstpara"
select="($paralist/self::para[@style='BULLET'])[1]"/>
    <xsl:apply-templates select="$paralist[not(. >> $firstpara)] except
$firstpara"/>
    <xsl:if test="$firstpara">
      <ul>
        <xsl:for-each-group select="$paralist[not($firstpara >> .)]"
          group-starting-with="para[not($firstpara/@style ne @style)]">
          <li>
            <xsl:apply-templates select="current-group()[1]"/>
            <xsl:if test="current-group()[2]">
              <xsl:call-template name="makeSubList">
                <xsl:with-param name="paralist"
select="current-group()[position()!=1]"/>
              </xsl:call-template>
            </xsl:if>
          </li>
        </xsl:for-each-group>
      </ul>
    </xsl:if>
  </xsl:template>

  <xsl:template name="makeSubList">
    <xsl:param name="paralist"/>
    <xsl:variable name="firstpara"
select="($paralist/self::para[@style='BULLINDENT'])[1]"/>
    <xsl:apply-templates select="$paralist[not(. >> $firstpara)] except
$firstpara"/>
    <xsl:if test="$firstpara">
      <ul>
        <xsl:for-each-group select="$paralist[not($firstpara >> .)]"
          group-starting-with="para[not($firstpara/@style ne @style)]">
          <li>
            <xsl:apply-templates select="current-group()[1]"/>
            <xsl:if test="current-group()[2]">
              <xsl:call-template name="makeSubList">
                <xsl:with-param name="paralist"
select="current-group()[position()!=1]"/>
              </xsl:call-template>
            </xsl:if>
          </li>
        </xsl:for-each-group>
      </ul>
    </xsl:if>
  </xsl:template>

It may be that this is completely the wrong way to go about it, so I'm open to
suggestions. I *think* they only have two levels of nesting but I noticed an
INDENT3 in their DTD so I am afraid I may need something more scalable.

Thanks!

Current Thread