我有一个XHTML文档,其中包含span和div元素,这些元素使用id和epub:type属性引用印刷版的分页符。例如:<div epub:type="pagebreak" id="page-3"/>。该文档还包含指向这些元素的链接,例如:<a href="#page-3">3</a>。
这个XHTML文档将被拆分成多个XHTML文档,以形成一个EPUB包。因此,需要更新href属性以匹配相应id的新位置。例如:<a href="02.xhtml#page-3">3</a>。新的XHTML文件的名称与body/section元素的位置相同。因此,在最后一个示例中,id="page-3"的分页符显然位于第二个body/section元素中。
我使用以下XSLT 2.0样式表:
<!--identity transform-->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!--variable to match id of elements with pagebreak values-->
<xsl:variable name="page-id" select="//*[@epub:type = 'pagebreak']/@id"/>
<!--update href attributes to match new filenames-->
<xsl:template match="a/@href">
<xsl:choose>
<xsl:when test="tokenize(., '#')[last()] = $page-id">
<xsl:attribute name="href">
<xsl:number count="//body/section[$page-id = tokenize(., '#')[last()]]" format="01"/>
<xsl:value-of select="concat('.xhtml', .)"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>它使用$page-id变量检查具有相应id的href属性。如果存在匹配项,则应使用count()函数更新href属性。否则,href应该保持不变。test似乎可以工作,但是,我没有得到我想要的结果。这是输入:
<body>
<section>
<p>Link to page 3: <a href="#page-3">3</a></p>
</section>
<section>
<div epub:type="pagebreak" id="page-3"/>
</section>
</body>这是我得到的输出:
<body>
<section>
<p>Link to page 3: <a href=".xhtml#page-3">3</a></p>
</section>
<section>
<div epub:type="pagebreak" id="page-3"/>
</section>
</body>这是我想要的输出:
<body>
<section>
<p>Link to page 3: <a href="02.xhtml#page-3">3</a></p>
</section>
<section>
<div epub:type="pagebreak" id="page-3"/>
</section>
</body>xsl:number中的XPath表达式似乎没有返回结果,但是我不知道为什么。有没有人能帮我一下?
发布于 2020-12-31 03:02:11
我想你想要例如:
<xsl:template match="body/section" mode="number">
<xsl:number format="01"/>
<xsl:template>然后不再是
<xsl:number count="//body/section[$page-id = tokenize(., '#')[last()]]" format="01"/>使用
<xsl:apply-templates select="key('page-id', substring-after(., '#'))" mode="number"/>外加一个键声明
<xsl:key name="page-id" match="body/section" use=".//*[@epub:type = 'pagebreak']/@id"/>https://stackoverflow.com/questions/65512043
复制相似问题