我有一个目录的XML。我正在为XML中的每个锚点从列表中生成命名的.xhtml页面。
TOC的结构包括各章,其中还列出了这些章节中的分节。我试图使用上一章锚的@href值动态地命名这些分段锚。
示例XML:
<div class="toc" id="s9781483331812.i34"><a class="page" id="pbr-v" title="v"/>
<p class="toc-title">Contents</p>
<ul class="toc">
<li class="toc-item">
<a class="ref-chap" href="#s132" id="s35"><b>Preface</b></a>
<a class="page-ref" href="#pbr-vii" id="s36"><b>vii</b></a>
</li>
<li class="toc-item">
<a class="ref-chap" href="#s135" id="s37"><b>Introduction</b></a>
<a class="page-ref" href="#pbr-xi" id="s38"><b>xi</b></a>
</li>
<li class="toc-item">
<span class="toc-label" title="1"><b>1.</b></span>
<a class="ref-chap" href="#s147" id="s39"><b>Chapter</b></a>
<a class="page-ref" href="#pbr-1" id="s40"><b>1</b></a>
<ul class="chapter-section">
<li class="toc-item">
<a class="ref-chap" href="#s152" id="s41">Subsection 1</a>
<a class="page-ref" href="#pbr-2" id="s42">2</a>
</li>
<li class="toc-item">
<a class="ref-chap" href="#s158" id="s43">Subsection 2</a>
<a class="page-ref" href="#pbr-6" id="s44">6</a>
</li>
<li class="toc-item">
<a class="ref-chap" href="#s159" id="s45">Subsection 3</a>
<a class="page-ref" href="#pbr-10" id="s46">10</a>
</li>
</ul>
</li>
</ul>
示例XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</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="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']//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='toc']//li[@class='toc-item']//a[@class='ref-chap'][last()]/@href, '#'),'.xhtml','#page',$pagenumber)"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
我想要的输出是:
<div class="toc" id="s9781483331812.i34">
<a class="page" id="pbr-v" title="v"/>
<p class="toc-title">Contents</p>
<ul class="toc">
<li>
<a href="s132.xhtml"><b>Preface</b></a>
<a class="page-ref" href="#pbr-vii" id="s36"><b>vii</b></a>
</li>
<li>
<a href="s135.xhtml"><b>Introduction</b></a>
<a class="page-ref" href="#pbr-xi" id="s38"><b>xi</b></a>
</li>
<li>
<span class="toc-label" title="1"><b>1.</b></span>
<a href="s147.xhtml"><b>Chapter</b></a>
<a class="page-ref" href="#pbr-1" id="s40"><b>1</b></a>
<ul class="chapter-section">
<li>
<a href="s147.xhtml#page2">Subsection 1</a>
<a class="page-ref" href="#pbr-2" id="s42">2</a>
</li>
<li>
<a href="s147.xhtml#page6">Subsection 2</a>
<a class="page-ref" href="#pbr-6" id="s44">6</a>
</li>
<li>
<a href="s147.xhtml#page10">Subsection 3</a>
<a class="page-ref" href="#pbr-10" id="s46">10</a>
</li>
</ul>
</li>
</ul>
我得到的错误是“不允许多个项的序列作为子字符串的第一个参数--后()(”#s 132“、”#s 135“、.)”。显然,我的XPath选择的是多个值,而不是一个。但是,我想不出怎么解决这个问题。
注:不知道章节的数量,可能会有变化。
发布于 2013-12-10 18:24:16
你的XPath表达式
ancestor::ul[@class='toc']//li[@class='toc-item']//a[@class='ref-chap'][last()]/@href肯定会太多--它将查看toc中所有toc-item列表项中的所有toc-item锚,并为您提供一个序列,其中包含所有这些锚,这些锚是它们各自父母中的最后一个ref-chap。因为没有一个toc-item包含多个ref-chap,这意味着所有的ref-chap。
假设您当前处于特定ref-chap的上下文中,您不需要一直走到toc级别,只需向上走到最近的chapter-section ul元素,然后取出该元素最近的同级ref-chap
ancestor::ul[@class='chapter-section'][1]/preceding-sibling::a[@class='ref-chap'][1]/@href注意,由于ancestor::和preceding-sibling::是反向轴,所以“最近的”匹配项(即按文档顺序排列的最后一个)是[1]。
https://stackoverflow.com/questions/20502086
复制相似问题