首页 > 解决方案 > XSLT 和 wkhtmltopdf TOC 将不同模板应用于不同深度的项目

问题描述

我正在尝试遍历 wkhtmltopdf 生成的 TOC 大纲中的项目内部的项目,并对每个项目应用不同的样式。

我尝试过的一件事是使用 3 个不同的模板进行以下操作:

<xsl:template match="outline:outline">
    <xsl:apply-templates select="outline:item/outline:item"/>
</xsl:template>

<xsl:template match="outline:item/outline:item">
    <xsl:if test="((@title!='') and (@title!='Table of Contents'))">
        <div style="width: 30%;">
            <h1> <xsl:value-of select="@title" /> </h1>
        </div>
        <div style="width: 70%;">
            <xsl:apply-templates select="outline:item"/>
        </div>
    </xsl:if>
</xsl:template>
<xsl:template match="outline:item/outline:item/outline:item">
    <h2> <xsl:value-of select="@title" /> </h2>
    <ul class="leaders">
        <xsl:apply-templates select="outline:item"/>
    </ul>
</xsl:template>
<xsl:template match="outline:item/outline:item/outline:item/outline:item">
    <li>
        <h3>
            <a>
                <xsl:if test="@link">
                    <xsl:attribute name="href"><xsl:value-of select="@link"/></xsl:attribute>
                </xsl:if>
                <xsl:if test="@backLink">
                    <xsl:attribute name="name"><xsl:value-of select="@backLink"/></xsl:attribute>
                </xsl:if>
                <span> <xsl:value-of select="@title" /> </span>
                <span> <xsl:value-of select="@page" /> </span>
            </a>
        </h3>
    </li>
</xsl:template>

我还尝试按模式或其他方式应用模板。我尝试过的另一个是<xsl:for-each>在彼此内部使用 3。作为参考,这是 wkhtmltopdf 生成的 xml 示例。

<?xml version="1.0" encoding="UTF-8"?>
<outline xmlns="http://wkhtmltopdf.org/outline">
    <item title="" page="0" link="" backLink=""/>
    <item title="" page="1" link="__WKANCHOR_0" backLink="__WKANCHOR_1">
        <item title="Table of Contents" page="3" link="__WKANCHOR_2" backLink="__WKANCHOR_3">
            <item title="H2 Test" page="3" link="test" backLink="test">
                <item title="H3 Test" page="3" link="test" backLink="test"/>
            </item>
        </item>
        <item title="Example Page 1" page="4" link="__WKANCHOR_4" backLink="__WKANCHOR_5"/>
        <item title="Example Page 2" page="5" link="__WKANCHOR_6" backLink="__WKANCHOR_7"/>
    </item>
</outline>

我的问题: 这样做并让它与 wkhtmltopdf 一起工作的正确方法是什么?到目前为止,使用 wkhtmltopdf 我没有得到 TOC 页面那部分的任何输出。使用我自己的 XSLT 解析器,我只得到<h1>输出,没有别的。我怎样才能解决这个问题?

编辑

根据要求,这是所需输出的示例。它是超级简化的,上面的代码也是如此,但总的来说,这是我希望它看起来的样子,带有适当的字体和其他样式。

<html>
    <head>
        <style>
            body {
                margin: 0 1in;
            }
            ul {
                list-style: none;
                margin: 0;
                padding: 0;
                overflow-x: hidden;
            }
            ul.leaders li:before {
                float: left;
                width: 0;
                white-space: nowrap;
                color: #003963;
                font-size: 1.6rem;
                content:
                        "........................................"
                        "........................................"
                        "........................................"
                        "........................................"
                        "........................................";
            }
            ul.leaders span:first-child {
                padding-right: 0.33em;
                background: white;
            }
            ul.leaders span + span {
                float: right;
                padding-left: 0.33em;
                background: white;
                position: relative;
                z-index: 10;
                font-size: 1rem;
            }
            ul.leaders span {
                margin-top: 0.5rem;
            }
            h1 {
                text-align: center;
                color: #96D1F2;
            }
            h2 {
                color: #F15A22;
            }
            h3 {
                color: #003963;
                text-transform: uppercase;
            }
        </style>
    </head>
    <body>
        <div>
            <div style="width: 30%; float: left; display: inline-block;">
                <h1>Big section</h1>
            </div>
            <div style="width: 70%; float: right; display: inline-block;">
                <h2>Sorta Big section</h2>
                <ul class="leaders">
                    <li>
                        <h3>
                            <span>Smaller Section</span>
                            <span>2</span>
                        </h3>
                    </li>
                    <li>
                        <h3>
                            <span>Smaller Section 2</span>
                            <span>3</span>
                        </h3>
                    </li>
                    <li>
                        <h3>
                            <span>Smaller Section 3</span>
                            <span>4</span>
                        </h3>
                    </li>
                </ul>
            </div>
        </div>
    </body>
</html>

编辑 2: 我已经在下面的答案中进行了建议的更新,并在 IntelliJ IDEA(我的 IDE)中为我的解析器修复了它,但它仍然存在与 wkhtmltopdf 中相同的问题,一旦它遇到 TOC,它就会挂起阶段。

标签: xmlxsltwkhtmltopdf

解决方案


您应该xsl:if在第二个模板中更改您的

<xsl:if test="((@title!='') and (@title!='Table of Contents'))">

<xsl:if test="@title!=''">

然后你的输出变为

<div style="width: 30%;">
    <h1>Table of Contents</h1>
</div>
<div style="width: 70%;">
    <h2 xmlns:outline="http://wkhtmltopdf.org/outline">H2 Test</h2>
    <ul xmlns:outline="http://wkhtmltopdf.org/outline" class="leaders">
        <li>
            <h3>
                <a href="test" name="test">
                    <span>H3 Test</span>
                    <span>3</span>
                </a>
            </h3>
        </li>
    </ul>
</div>
<div style="width: 30%;">
    <h1>Example Page 1</h1>
</div>
<div style="width: 70%;"/>
<div style="width: 30%;">
    <h1>Example Page 2</h1>
</div>
<div style="width: 70%;"/>

这非常接近预期的结果。
我无法进一步改进模板,因为您想要的结果差异太大。但我想你现在可以相处了。


推荐阅读