XSLT 和 wkhtmltopdf 目录将不同的模板应用于不同深度的项目



我正在尝试循环浏览wkhtmltopdf生成的目录大纲中的项目内部的项目,并对每个项目应用不同的样式。

我尝试过的一件事是以下 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 个<xsl:for-each>。作为参考,这是由 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 解析器,我只能获得<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阶段,它就会挂起。

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

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

最新更新