得到祖先的后代节点的位置,不一定是兄弟(get the position of a descenda

2019-10-22 10:55发布

在下面的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:号码,但无法得到任何工作。 我能得到的最接近的是以下几点:

<?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的部分。 有任何想法吗?

Answer 1:

这会为你工作?

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


文章来源: get the position of a descendant node from ancestor, not necessarily a sibling
标签: xml xslt xpath