tags:

views:

348

answers:

3
<a>
   <b/>
   <c/>
   <d/>
   <b/>
   <e/>
</a>

How do I select those children of "a" that are not "b"?

+7  A: 

/a/*[not(self::b)]

AakashM
+1 for the nice usage of the `self` axis ;)
Lucero
@Lucero: I owe @Tomalak for that idea :)
AakashM
Thanks for the credit. :)
Tomalak
When I run your XPath, I got only c,d,e nodes. But, node b is not included in it. How to get the node b also in the list excluding its children?
Sailaja
@Sailaja : I think you should make your own separate question. When you do so, be sure to explain what you mean by getting node b but not its children - when xpath picks out a node, the node still *has* its children...
AakashM
A: 

Xpath will look:

a/*[name(.) !='b']

So, select children of 'a' whose name is not equal 'b'

Dewfy
This breaks when namespaces and prefixes are being used.
Lucero
@Lucero - according to source XML there is no namespace, but if so you have "local-name" function
Dewfy
@Dewfy: yeah, and local-name() breaks also because it will match elements from any namespace instead of a specific element name. Since samples posted here are often simplified (quite obvious here) I'd not be so sure that no namespaces will be used, and even then if someone googles and comes across this answer they should be aware that your solution does not work with namespaces.
Lucero
+1  A: 

With XPath 2.0 you can even do

/a/(* except b)
Martin Honnen