Kahn
Kahn

Reputation: 1660

How to ignore namespaces in XPath 1.0?

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?

Upvotes: 1

Views: 738

Answers (1)

kjhughes
kjhughes

Reputation: 111621

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.

Upvotes: 3

Related Questions