Xslt 将属性值更改为具有相应属性值的另一个元素的位置
我有一个XHTML文档,其中包含Xslt 将属性值更改为具有相应属性值的另一个元素的位置,xslt,xslt-2.0,Xslt,Xslt 2.0,我有一个XHTML文档,其中包含span和div元素,这些元素使用id和epub:type属性引用打印版本的分页符。例如:。该文档还具有指向这些元素的链接,例如: 此单个XHTML文档将被拆分为多个XHTML文档以形成一个EPUB包。因此,需要更新href属性以匹配相应id的新位置。例如:。新XHTML文件的名称等于主体/部分元素的位置。因此,在上一个示例中,带有id=“page-3”的分页符显然位于第二个body/section元素中 我正在使用以下XSLT 2.0样式表: <!--id
span
和div
元素,这些元素使用id
和epub:type
属性引用打印版本的分页符。例如:
。该文档还具有指向这些元素的链接,例如:
此单个XHTML文档将被拆分为多个XHTML文档以形成一个EPUB包。因此,需要更新href
属性以匹配相应id
的新位置。例如:
。新XHTML文件的名称等于主体/部分
元素的位置。因此,在上一个示例中,带有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>
这是我得到的输出:
<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>
链接至第3页:
这是我想要的输出:
<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>
链接至第3页:
似乎xsl:number
中的XPath表达式没有返回结果,但我不知道为什么。有谁能帮我做这个吗?我想你需要
<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:key name="page-id" match="body/section" use=".//*[@epub:type = 'pagebreak']/@id"/>
这似乎有效,谢谢!我以前没有使用过mode属性,所以我将仔细阅读。
<xsl:apply-templates select="key('page-id', substring-after(., '#'))" mode="number"/>
<xsl:key name="page-id" match="body/section" use=".//*[@epub:type = 'pagebreak']/@id"/>