XSL-FO - 如何在两个列表块中继续编号?

时间:2011-09-08 15:27:44

标签: list xslt xsl-fo docbook

有人知道如何通过XSL-FO转换实现这一目标吗?问题详情应从下面的代码中清楚。

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>&#8226;</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>&#8226;</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>

2 个答案:

答案 0 :(得分:1)

假设您输入样本是(添加部分最顶层元素以使样本格式良好):

<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>

在第一级的给定 listitem 中,您可以使用:

count(
    preceding-sibling::listitem 
    | 
    ../../preceding-sibling::section/orderedlist/listitem) 
    + 1

例如:

<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.&#x20;"/>
        </fo:list-item>
    </xsl:template>
</xsl:stylesheet>

产生

<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>

答案 1 :(得分:0)

您是否有特殊原因不使用DocBook和DocBook-XSL样式表?这会给你很多“免费”(也许你已经知道了)。

在DocBook中,您要求的内容已经实现。 <orderedlist>上有一个continuation属性,用于指示列表中的编号是否从前一个列表继续。这在FO输出的DocBook-XSL样式表中受支持(请查看fo / lists.xsl和common / common.xsl以了解它是如何完成的)。