xml - 获取祖先节点的后代节点的位置,不一定是兄弟节点

标签 xml xslt xpath

在下面的 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/

相关文章:

jquery - 使用 JQuery 解析 XML

XSLT/Diazo : create element for each pair of elements

java - 使用 Xpath 表达式和 jaxb 解码 XML

java - WebDriver/PageObject/FindBy : how to specify xpath with dynamic value?

xml - 按属性排列的 XSLT 子级列表

xslt - 用于获取数据类型 QName 的 XML 属性的本地名称和命名空间的 XPATH 表达式

c# - InvalidArgument= '0' 的值对于 'SelectedIndex' 无效。参数名称 : SelectedIndex

xml - 覆盖 "setTemplate"Magento xml

android - 如何将操作栏中的按钮与导航 View 正确对齐

java - 使用 XSLT 从 XML 生成 EXCEL