【问题标题】:XSL:FO Different list-item-label per levelXSL:FO 每个级别不同的列表项标签
【发布时间】:2017-04-22 05:49:44
【问题描述】:

我正在使用 Apache FOP 从我的 Web 应用程序生成 PDF,用户可以在其中使用 CKEditor 编辑富文本。

我的问题是用户有时会在(无)有序列表中使用不同级别的缩进,例如:

  • 列表项级别 1
    • 2 级列表项

CKEditor 显示每个级别(或缩进)的不同公告,但生成的 PDF 不显示,因为我的模板如下所示:

<!-- Lists -->
<xsl:template match="ul|ol" mode="content">
    <xsl:apply-templates select="li" mode="content">
        <xsl:with-param name="ordered">
            <xsl:value-of select="name()='ol'"/>
        </xsl:with-param>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="li" mode="content">
    <xsl:param name="ordered"/>
    <xsl:variable name="label">
        <xsl:choose>
            <xsl:when test="$ordered='true'">
                <xsl:value-of select="position()"/>
                .
            </xsl:when>
            <xsl:otherwise>
                &#8226;
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <fo:list-block padding-bottom="0pt">
        <fo:list-item>
            <fo:list-item-label>
                <fo:block>
                    <xsl:value-of select="$label"/>
                </fo:block>
            </fo:list-item-label>
            <fo:list-item-body start-indent="body-start()">
                <fo:block>
                    <xsl:apply-templates mode="content"/>
                </fo:block>
            </fo:list-item-body>
        </fo:list-item>
    </fo:list-block>
</xsl:template>

那么如何根据我所处的缩进级别设置标签变量?

类似:

  • 第一级:•
  • 第二级:◦
  • 第三级:⁍

提前致谢,~法比

编辑:所以@fafl 建议的解决方案如下所示:

<!-- Lists -->
    <xsl:template match="ul|ol" mode="content">
        <xsl:param name="depth" select="0"/>
        <xsl:apply-templates select="li" mode="content">
            <xsl:with-param name="ordered">
                <xsl:value-of select="name()='ol'"/>
            </xsl:with-param>
            <xsl:with-param name="depth">
                <xsl:value-of select="$depth + 1"/>
            </xsl:with-param>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="li" mode="content">
        <xsl:param name="depth" select="0"/>
        <xsl:param name="ordered"/>
        <xsl:variable name="label">
            <xsl:choose>
                <xsl:when test="$ordered='true'">
                    <xsl:value-of select="position()"/>
                    .
                </xsl:when>
                <xsl:otherwise>
                    <xsl:choose>
                        <xsl:when test="$depth = 1">
                            &#8226; <!--filled circle-->
                        </xsl:when>
                        <xsl:when test="$depth = 2">
                            &#9702; <!--not-filled circle-->
                        </xsl:when>
                        <xsl:otherwise>
                            &#9632; <!--black square -->
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <fo:list-block padding-bottom="0pt">
            <fo:list-item>
                <fo:list-item-label>
                    <fo:block>
                        <xsl:value-of select="$label"/>
                    </fo:block>
                </fo:list-item-label>
                <fo:list-item-body start-indent="body-start()">
                    <fo:block>
                        <xsl:apply-templates mode="content">
                            <xsl:with-param name="depth">
                                <xsl:value-of select="$depth"/>
                            </xsl:with-param>
                        </xsl:apply-templates>
                    </fo:block>
                </fo:list-item-body>
            </fo:list-item>
        </fo:list-block>
    </xsl:template>

【问题讨论】:

    标签: xslt pdf-generation html-lists apache-fop


    【解决方案1】:

    尝试向两个模板添加参数“depth”,默认值为 1。在每次递归调用“ul|ol”时,将其加 1。然后您可以在两个模板中查询它。

    【讨论】:

    • 这很好用。谢谢。我应该(如何)为未来的访问者添加完成的模板?
    猜你喜欢
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多