如何忽略 XPath 1.0 中的命名空间? [英] How to ignore namespaces in XPath 1.0?

查看:27
本文介绍了如何忽略 XPath 1.0 中的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在试图理解 XPath 的魔力时遇到了严重的问题.

I'm having serious issues trying to understand the magic that is XPath.

基本上,我有一些像这样的 XML:

Basically, I have some XML like so:

<a>
    <b>
        <c/>
    </b>
</a>

现在,我想计算我们有多少个 B,没有 C.这可以使用以下 XPath 轻松完成:

Now, I want to count how many B's we have, without C's. This can be done easily with the following XPath:

count(*/b[not(descendant::c)])

现在的问题很简单:我如何做同样的事情,同时忽略任何命名空间?

Now the question is this simple: How do I do the same thing, while ignoring any namespaces?

我会想象它是这样的吗?

I'd imagine it was something like this?

count(*/[local-name()='b']/[not(descendant::[local-name()='c'])])

但这并不正确.什么是我上面的等效 XPath 但忽略命名空间?

But this is not correct. What would be the equivalent XPath as I have above but that ignores namespaces?

推荐答案

给定的 XPath,

count(*/b[not(descendant::c)])

可以重写为忽略命名空间,如下所示:

can be re-written to ignore namespaces as follows:

count(*[local-name()='b' and not(descendant::*[local-name()='c'])])

请注意,通常最好不要破坏命名空间,而是通过分配命名空间前缀并在 XPath 表达式中使用它们来正确使用它们.

Note that it generally is better not to defeat namespaces but to work with them properly by assigning namespace prefixes and using them in your XPath expression.

这篇关于如何忽略 XPath 1.0 中的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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