[xsl] Nested list handling in WordML

Subject: [xsl] Nested list handling in WordML
From: "Joga Singh Rawat jrawat@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 18 Aug 2015 05:07:13 -0000
Dear experts,
I am not able to handle nested listing of WordML. Below is the details
description. Could anybody let me correct the below approach or an suggest
another approach.

Input
<w:p><w:pPr><w:listPr><w:ilvl w:val="0"/><wx:t
wx:val="1"/></w:listPr></w:pPr><w:r><w:t>first level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="0"/><wx:t
wx:val="2"/></w:listPr></w:pPr><w:r><w:t>first level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="1"/><wx:t
wx:val="a"/></w:listPr></w:pPr><w:r><w:t>second level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="1"/><wx:t
wx:val="b"/></w:listPr></w:pPr><w:r><w:t>second level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="2"/><wx:t
wx:val="i"/></w:listPr></w:pPr><w:r><w:t>third level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="2"/><wx:t
wx:val="ii"/></w:listPr></w:pPr><w:r><w:t>third level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="1"/><wx:t
wx:val="c"/></w:listPr></w:pPr><w:r><w:t>second level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="1"/><wx:t
wx:val="d"/></w:listPr></w:pPr><w:r><w:t>second level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="0"/><wx:t
wx:val="3"/></w:listPr></w:pPr><w:r><w:t>first level</w:t></w:r></w:p>
<w:p><w:pPr><w:listPr><w:ilvl w:val="0"/><wx:t
wx:val="4"/></w:listPr></w:pPr><w:r><w:t>first level</w:t></w:r></w:p>

Current output
<ol>
<li><label>1</label><p>first level</p></li>
<li><label>2</label><p>first level</p></li>
<li level="2"><label>a</label><p>second level</p></li>
<li level="2"><label>b</label><p>second level</p></li>
<li level="3"><label>i</label><p>third level</p></li>
<li level="3"><label>ii</label><p>third level</p></li>
<li level="2"><label>c</label><p>second level</p></li>
<li level="2"><label>d</label><p>second level</p></li>
<li><label>3</label><p>first level</p></li>
<li><label>4</label><p>first level</p></li>
</ol>

Expected output
<ol>
<li><label>1</label><p>first level</p></li>
<li><label>2</label><p>first level</p>
<ol>
  <li level="2"><label>a</label><p>second level</p></li>
  <li level="2"><label>b</label><p>second level</p>
  <ol>
    <li level="3"><label>i</label><p>third level</p></li>
    <li level="3"><label>ii</label><p>third level</p></li>
   </ol></li>
   <li level="2"><label>c</label><p>second level</p></li>
    <li level="2"><label>d</label><p>second level</p></li>
   </ol></li>
<li><label>3</label><p>first level</p></li>
<li><label>4</label><p>first level</p></li>
</ol>

XSLT
<xsl:template match="w:p">
  <xsl:choose>
   <xsl:when test="w:pPr/w:listPr">
    <xsl:if test="not(preceding-sibling::*[1][self::w:p/w:pPr/w:listPr])">
     <xsl:variable name="list-type">
      <xsl:call-template name="GetListType">
       <xsl:with-param name="listIndexerFont"
select="w:pPr/w:listPr/wx:font/@wx:val"/>
      </xsl:call-template>
     </xsl:variable>
     <xsl:element name="{$list-type}">
      <li>
       <xsl:if test="descendant::wx:t/@wx:val">
        <xsl:variable name="label">
         <xsl:call-template name="replace-entities">
          <xsl:with-param name="text" select="descendant::wx:t/@wx:val" />
         </xsl:call-template>
        </xsl:variable>
        <label>
         <xsl:apply-templates select="$label"/>
        </label>
       </xsl:if>
       <p><xsl:apply-templates select="w:r"/></p>
      </li>
      <xsl:apply-templates
select="following-sibling::*[1][self::w:p[w:pPr/w:listPr/w:ilvl/@w:val='0']]
| 
 
following-sibling::*[1][self::w:p[w:pPr/w:listPr]]" mode="listing"/>
     </xsl:element>
    </xsl:if>
   </xsl:when>
  </xsl:choose>
 </xsl:template>

<xsl:template match="*" mode="listing">
  <li>
   <xsl:choose>
    <xsl:when test="w:pPr/w:listPr/w:ilvl/@w:val='1'">
     <xsl:attribute name="level" select="'2'"/>
    </xsl:when>
    <xsl:when test="w:pPr/w:listPr/w:ilvl/@w:val='2'">
     <xsl:attribute name="level" select="'3'"/>
    </xsl:when>
   </xsl:choose>
   <xsl:if test="descendant::wx:t/@wx:val">
    <xsl:variable name="label">
     <xsl:call-template name="replace-entities">
      <xsl:with-param name="text" select="descendant::wx:t/@wx:val" />
     </xsl:call-template>
    </xsl:variable>
    <label>
     <xsl:apply-templates select="$label"/>
    </label>
   </xsl:if>
   <p><xsl:apply-templates select="w:r"/></p>
  </li>
  <xsl:apply-templates
select="following-sibling::*[1][self::w:p[w:pPr/w:listPr]]" mode="listing"/>
 </xsl:template>

Thanks a lot in advance.

...JSR

Current Thread