我正在尝试通过 XSLT 从 XML 文件生成 EPUB3 导航文档。
示例 XML:
<div class="toc" id="s1">
<p class="toc-title">Detailed Contents</p>
<ul class="toc">
<li class="toc-item">
<a class="ref-chap" id="a1" href="#a1">Preface</a>
</li>
<li class="book-section">
<a class="ref-chap" id="a2" href="#a2">
<b>Part I.</b>
</a>
<ul class="book-section">
<li class="toc-item">
<a class="ref-chap" id="a3" href="#a3">
<b>Chapter 1</b>
</a>
<ul class="chapter-section">
<li class="toc-item">
<a class="ref-chap" id="a4" href="#a4">
<b>Sub Chapter a</b>
</a>
<ul class="chapter-subsection">
<li class="toc-item">
<a class="ref-chap" id="a5" href="#a5">Sub Chapter B</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="book-section">
<li class="toc-item">
<a class="ref-chap" id="a6" href="#a6">
<b>Chapter 2</b>
</a>
<ul class="chapter-section">
<li class="toc-item">
<a class="ref-chap" id="a7" href="#a7">
<b>Sub Chapter A</b>
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
需要的输出:
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
<h1>Detailed Contents</h1>
<ol>
<li><a href="a1">Preface: To Our Readers</a></li>
<li><a href="a2"><b>Part I</b></a>
<ol>
<li><a href="a3"><b>Chapter 1</b></a>
<ol>
<li><a href="a4"><b>Sub Chapter A</b></a>
<ol>
<li><a href="a5">Sub Chapter B</a></li>
</ol>
</li>
</ol>
</li>
<li><a href="a6"><b>Chapter 2</b></a>
<ol>
<li><a href="a7"><b>Sub Chapter A</b></a></li>
</ol>
</li>
</ol>
</li>
</ol>
</nav>
我的问题区域是<ol>
第一部分的孩子,我需要它在第 2 章之后关闭,但它在第 1 章结束后关闭,而不是使用以下 XSLT。
我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:epub="http://www.idpf.org/2007/ops"
exclude-result-prefixes="xs epub"
version="2.0">
<xsl:output method="xhtml" include-content-type="no" xpath-default-namespace="http://www.w3.org/1999/xhtml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="div[@class='toc']">
<xsl:element name="nav">
<xsl:attribute name="epub:type" select="'toc'"/>
<xsl:attribute name="id" select="'toc'"/>
<xsl:element name="h1">
<xsl:value-of select="//div[@class='toc']/p[@class='toc-title']"/>
</xsl:element>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="div[@class='toc']//ul[@class='toc'] | div[@class='toc']//ul[@class='book-section'] | div[@class='toc']//ul[@class='chapter-section' or @class='chapter-subsection']">
<xsl:element name="ol">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="div[@class='toc']//li[@class='toc-item']">
<xsl:element name="li">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="div[@class='toc']//li[@class='book-section']">
<xsl:element name="li">
<xsl:apply-templates xml:space="default"/>
</xsl:element>
</xsl:template>
<xsl:template match="a[@class='ref-chap']">
<xsl:element name="a">
<xsl:attribute name="href" select="concat(substring-after(@href, '#'),'.xhtml')"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template priority="1" match="ul[@class='chapter-section' or @class='chapter-subsection']//a[@class='ref-chap']">
<xsl:variable name="pagenumber" select="following-sibling::a[@class='page-ref']"/>
<xsl:element name="a">
<xsl:attribute name="href" select="concat(substring-after(ancestor::ul[@class='chapter-section'][1]/preceding-sibling::a[@class='ref-chap'][1]/@href, '#'),'.xhtml','#page',$pagenumber)"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
给我一个无效的 EPUB3 导航文档:
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
<h1>Detailed Contents</h1>Detailed Contents
<ol>
<li>
<a href="a1.xhtml">Preface</a>
</li>
<li>
<a href="a2.xhtml">Part I.</a>
<ol>
<li>
<a href="a3.xhtml">Chapter 1</a>
<ol>
<li>
<a href="a3.xhtml#page">Sub Chapter a</a>
<ol>
<li>
<a href="a3.xhtml#page">Sub Chapter B</a>
</li>
</ol>
</li>
</ol>
</li>
**</ol>
<ol>**
<li>
<a href="a6.xhtml">Chapter 2</a>
<ol>
<li>
<a href="a6.xhtml#page">Sub Chapter A</a>
</li>
</ol>
</li>
</ol>
</li>
</ol>
</nav>
添加了强调**以指示这些标签是问题所在。它们不应出现在输出中。