在下面的 xml 片段中,我有一个带脚注的部分和一个带脚注的小节。我想从论文/章节级别开始按顺序对脚注重新编号,尽管 fn 不是同级
<?xml version='1.0' ?>
<paper>
<section>
<title>My Main Section</title>
<para>My para with a <footnote num="1">text</footnote> footnote.</para>
<section>
<title>my subsection</title>
<para>more text with another <footnote num="1">more fn text.</footnote> footnote.</para>
</section>
</section>
</paper>
预期输出为:
<?xml version='1.0' ?>
<paper>
<section><title>My Main Section</title>
<para>My para with a <footnote num="1">text</footnote> footnote.</para>
<section><title>my subsection</title>
<para>more text with another <footnote num="2">more fn text.</footnote> footnote.</para>
</section>
</section>
</paper>
我尝试使用 xsl:number 进行各种操作,但没有任何效果。我能得到的最接近的是:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="footnote/@num">
<xsl:attribute name="num"><xsl:value-of select="count(ancestor::paper/section//footnote)"/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
这给出了正确的计数 2,但我不确定如何指示“我是本主要部分中两个脚注中的第一个”。
我还尝试编写一个命名模板,如下所示:
<xsl:template match="/paper/section">
<section>
<xsl:call-template name="renumberFNs"/>
<xsl:apply-templates/>
</section>
</xsl:template>
<xsl:template name="renumberFNs">
<xsl:for-each select=".//footnote/@num">
<xsl:attribute name="num"><xsl:value-of select="position()"/></xsl:attribute>
</xsl:for-each>
</xsl:template>
但这将 @num 放在该部分上。有什么想法吗?
最佳答案
这对你有用吗?
<xsl:template match="footnote/@num">
<xsl:attribute name="num">
<xsl:number count="footnote" level="any" from="paper/section"/>
</xsl:attribute>
</xsl:template>
关于xml - 获取祖先节点的后代节点的位置,不一定是兄弟节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372876/