Does somebody know how to achieve this with XSL-FO transformation? The question details should be clear from the codes below.
Input:
<section>
<title>Section 1</title>
<orderedlist>
<listitem><para>item 1.1</para></listitem>
<listitem>
<para>item 1.2</para>
<orderedlist>
<listitem><para>item a</para></listitem>
<listitem><para>item b</para></listitem>
</orderedlist>
</listitem>
</orderedlist>
</section>
<section>
<title>Section 2</title>
<orderedlist>
<listitem><para>item 2.1</para></listitem>
<listitem><para>item 2.2</para></listitem>
</orderedlist>
</section>
Desired output:
Section 1
1. item 1.1
2. item 1.2
a. item a
b. item b
Section 2
3. item 2.1
4. item 2.2
Here is the XSL file for lists:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- templates for lists - supports numbered and itemized types -->
<!-- the maximum depth is currently 2 -->
<xsl:template match="orderedlist">
<fo:list-block start-indent="0.5cm" space-before="0.2cm"
provisional-distance-between-starts="0.7cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="orderedlist//orderedlist">
<fo:list-block start-indent="1.2cm" provisional-distance-between-starts="0.7cm"
padding-top="-0.2cm" padding-bottom="0.2cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="itemizedlist">
<fo:list-block start-indent="0.5cm" space-before="0.2cm"
provisional-distance-between-starts="0.7cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="itemizedlist//itemizedlist">
<fo:list-block start-indent="1.2cm" provisional-distance-between-starts="0.7cm"
padding-top="-0.2cm" padding-bottom="0.2cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="orderedlist/listitem">
<fo:list-item margin-top="0.1cm">
<fo:list-item-label end-indent="label-end()">
<fo:block>
<xsl:number count="listitem" format="1." />
</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match="orderedlist//orderedlist/listitem">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block>
<xsl:number count="listitem" format="a." />
</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match="itemizedlist/listitem">
<fo:list-item margin-top="0.1cm">
<fo:list-item-label end-indent="label-end()">
<fo:block>•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match="itemizedlist//itemizedlist/listitem">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block>•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
</xsl:stylesheet>
Assuming you input sample is (added the sections topmost element to make the sample well-formed):
<sections>
<section>
<title>Section 1</title>
<orderedlist>
<listitem><para>item 1.1</para></listitem>
<listitem>
<para>item 1.2</para>
<orderedlist>
<listitem><para>item a</para></listitem>
<listitem><para>item b</para></listitem>
</orderedlist>
</listitem>
</orderedlist>
</section>
<section>
<title>Section 2</title>
<orderedlist>
<listitem><para>item 2.1</para></listitem>
<listitem><para>item 2.2</para></listitem>
</orderedlist>
</section>
</sections>
at a given listitem of first level you can use:
count(
preceding-sibling::listitem
|
../../preceding-sibling::section/orderedlist/listitem)
+ 1
For example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output indent="yes"/>
<xsl:template match="sections">
<fo:block>
<xsl:apply-templates select="section"/>
</fo:block>
</xsl:template>
<xsl:template match="section">
<fo:list-block>
<xsl:apply-templates select="title | orderedlist/listitem"/>
</fo:list-block>
</xsl:template>
<xsl:template match="title">
<fo:list-item>
<xsl:value-of select="."/>
</fo:list-item>
</xsl:template>
<xsl:template match="listitem">
<fo:list-item>
<xsl:number
value="count(
preceding-sibling::listitem
|
../../preceding-sibling::section/orderedlist/listitem)
+ 1" format="1. "/>
</fo:list-item>
</xsl:template>
</xsl:stylesheet>
produces:
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:list-block>
<fo:list-item>Section 1</fo:list-item>
<fo:list-item>1. </fo:list-item>
<fo:list-item>2. </fo:list-item>
</fo:list-block>
<fo:list-block>
<fo:list-item>Section 2</fo:list-item>
<fo:list-item>3. </fo:list-item>
<fo:list-item>4. </fo:list-item>
</fo:list-block>
</fo:block>