How to ignore namespaces in XPath 1.0?

冷暖自知 提交于 2021-01-28 19:35:08

问题


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

Basically, I have some XML like so:

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

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'])])

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


回答1:


The given 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'])])

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.



来源:https://stackoverflow.com/questions/39852979/how-to-ignore-namespaces-in-xpath-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!