XPath - 确定元素位置 [英] XPath - determine the element position

查看:25
本文介绍了XPath - 确定元素位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每个表创建一个索引(确定在 XML 中的位置),但问题是这些表的深度不同.我计划用 XSLT 转换到 FO 来处理 XML.我知道如何做到这一点吗?

I want to create an index (determine the position in the XML) for every table but the problem is that the tables are in different depth. I plan to process the XML with XSLT transformation to FO. I Any ideas how to do this?

示例 XML

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

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

        <subsection>
            <table> ... </table>
        </subsection>
    </section>
</document>

推荐答案

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

@Tomalak's solution isn't completely correct and will produce wrong result in the case when there are nested tables.

这样做的原因是 XPath precedingancestor 轴不重叠.

The reason for this is that the XPath preceding and ancestor axes are non-overlapping.

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

count(ancestor::table | preceding::table) + 1

所以,使用:

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

这篇关于XPath - 确定元素位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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