从祖先获取后代节点的位置,不一定是兄弟节点 [英] get the position of a descendant node from ancestor, not necessarily a sibling

查看:27
本文介绍了从祖先获取后代节点的位置,不一定是兄弟节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的 xml 片段中,我有一个带脚注的部分和一个带脚注的小节.我想从论文/章节级别开始按顺序重新编号脚注,尽管 fn 不是兄弟姐妹

In the following xml snippet I have a section with a footnote, and a subsection with a footnote. I want to renumber the footnotes sequentially starting at the paper/section level, despite the fact that the fn's are not siblings

<?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 进行各种操作,但没有任何效果.我能得到的最接近的是以下内容:

I was trying various things with xsl:number, but couldn't get anything to work. The closest I could get was the following:

<?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,但我不知道如何表示我是这个主要部分中两个脚注中的第一个".

that gives the correct count of 2, but I'm not sure how to indicate that "I am the first of two footnotes in this main section".

我也尝试编写一个命名模板,如下所示:

I also tried writing a named template like so:

<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 放在该部分.有什么想法吗?

but that put the @num on the section. Any ideas?

推荐答案

这对你有用吗?

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

这篇关于从祖先获取后代节点的位置,不一定是兄弟节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆