如何将XML中的tei:note转换为HTML脚注并在页脚展示内容?
实现TEI XML到HTML脚注的完整转换
你需要在footer中单独处理所有tei:note元素,并且通过**模式(mode)**区分正文和脚注两种不同的渲染逻辑,具体修改如下:
1. 修改正文的tei:note模板(添加返回锚点)
为了支持从脚注返回正文引用位置,给正文里的脚注链接加上唯一ID:
<xsl:template match="tei:note"> <sup> <a href="#note{@n}" id="ref{@n}"> <xsl:value-of select="@n"/> </a> </sup> </xsl:template>
2. 更新根节点模板的footer部分
将footer中的xsl:apply-templates路径改为选中所有正文内的tei:note元素,并指定mode="footer":
<xsl:template match="/tei:TEI"> <html> <head> <!-- Transform teiHeader elements --> <xsl:apply-templates select="tei:teiHeader"/> <xsl:apply-templates select="tei:standOff"/> </head> <body> <hr /> <!-- Transform text elements --> <p><h2>Transcription</h2></p> <xsl:apply-templates select="tei:text"/> </body> <footer> <h3>脚注</h3> <!-- 选中所有正文里的note元素,用footer模式处理 --> <xsl:apply-templates select="tei:text/tei:body//tei:note" mode="footer"/> </footer> </html> </xsl:template>
3. 编写脚注模式的tei:note模板
这个模板负责渲染footer中的脚注内容,包含匹配正文链接的锚点ID、脚注编号、内容以及返回链接:
<xsl:template match="tei:note" mode="footer"> <div id="note{@n}" class="footnote"> <sup><xsl:value-of select="@n"/></sup> <!-- 渲染note标签内的所有内容 --> <xsl:apply-templates/> <!-- 返回正文引用位置的链接 --> <a href="#ref{@n}" style="margin-left: 8px;">↑</a> </div> </xsl:template>
关键说明
- 使用
mode="footer"确保同一个tei:note元素在正文和footer中被不同模板处理,避免冲突。 - 通过
@n属性建立正文引用和脚注内容的对应关系,保证链接跳转正确。 - 新增的返回链接可以让读者从脚注快速回到正文的引用位置,提升阅读体验。
内容的提问来源于stack exchange,提问作者Jaap
相关产品推荐
相关产品推荐

