XPath - 确定元素位置

use*_*882 2 xml xslt xpath

我想为每个表创建一个索引(确定XML中的位置),但问题是表的深度不同.我计划使用XSLT转换处理XML到FO.我有什么想法怎么做?

Sample XML

<document>
    <table> ... </table>

    <section>
        <table> ... </table>

        <subsection>
            <table> ... </table>
        </subsection>
    </section>
</document>
Run Code Online (Sandbox Code Playgroud)

Dim*_*hev 5

@ Tomalak的解决方案不完全正确,并且在存在嵌套表的情况下会产生错误的结果.

原因是XPath precedingancestor轴不重叠.

一个正确的XPath表达式给出了想要的数字是:

count(ancestor::table | preceding::table) + 1
Run Code Online (Sandbox Code Playgroud)

所以,使用:

<xsl:template match="table">
    <table id="tbl_{count(ancestor::table | preceding::table) + 1}">
        <!-- further processing -->
    </table>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)