【问题标题】:XSLT and wkhtmltopdf TOC Applying Different Templates to Item at Different DepthXSLT 和 wkhtmltopdf TOC 将不同模板应用于不同深度的项目
【发布时间】:2019-03-21 22:44:11
【问题描述】:

我正在尝试遍历 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>

我也尝试过按模式或其他方式应用模板。我尝试过的另一个方法是在彼此内部使用 3 &lt;xsl:for-each&gt;。作为参考,这是 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 我没有得到任何输出目录页的。使用我自己的 XSLT 解析器,我只得到 &lt;h1&gt; 输出,没有别的。我该如何解决这个问题?

编辑

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

<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 阶段时它只是挂起。

【问题讨论】:

  • 您想要的 XML 输出是什么样的?您可以使用哪个 XSLT 版本?
  • 我试图弄清楚 wkhtmltopdf 使用什么 XSLT 版本,我能找到的最好的是它使用 QTWebKit 来完成整个事情。我试图弄清楚是否有某种参考,但我找不到。至于 XML 输出,我正在用一个例子来编辑我的问题。
  • 如果您找不到任何信息,最好使用 1.0 版本。但优先级应该放在预期的结果上。
  • "我的问题正确的方法是什么?跨度>

标签: xml xslt wkhtmltopdf


【解决方案1】:

您应该将第二个模板中的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%;"/>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2018-06-07
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多