【问题标题】:get the position of a descendant node from ancestor, not necessarily a sibling从祖先获取后代节点的位置,不一定是兄弟节点
【发布时间】:2015-03-31 15:48:52
【问题描述】:

在下面的 xml sn-p 中,我有一个带脚注的部分和一个带脚注的小节。我想从论文/章节级别开始按顺序重新编号脚注,尽管事实上 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 放在该部分上。有什么想法吗?

【问题讨论】:

  • 从论文/章节级别开始重新编号脚注,因此 fn 不是兄弟姐妹”我不确定这意味着什么。为什么不发布转换的预期结果?
  • 添加了预期的输出,并为清晰起见进行编辑(我希望 :))

标签: xml xslt xpath


【解决方案1】:

这对你有用吗?

<xsl:template match="footnote/@num">
    <xsl:attribute name="num">
        <xsl:number count="footnote" level="any" from="paper/section"/>
    </xsl:attribute>
</xsl:template>

【讨论】:

  • 完美,谢谢。对自己没有从我搜索的文档中找到它而感到恼火
猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 2021-05-18
  • 1970-01-01
  • 2014-02-05
  • 2023-03-14
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
相关资源
最近更新 更多