jrharshath
jrharshath

Reputation: 26583

How do I select nodes that DO NOT have a particular child node?

A rather simple problem... XML Fragment:

<bean id='Juicer'>
    <property name="electric">
        <value>false</value>
    </property>
</bean>

<bean id='Oven'>
    <property name="electric">
        <value>true</value>
    </property>
    <property name="wattage">
        <value>1000</value>
    </property>
</bean>

I'm trying to write an xpath query that will select all beans that do not have a <property name="wattage">.

I cant figure out how to say "beans not having this child" in xpath.

Note that I cannot rely on the "electric" property to be false each time the "wattage" is absent. (also, this example is kinda contrived).

Thanks :)

Upvotes: 6

Views: 3552

Answers (4)

aaa
aaa

Reputation: 1

//bean[not(@name = 'wattage')]

Upvotes: 0

jrharshath
jrharshath

Reputation: 26583

Okay, after a little digging i figured it out:

//bean[not (property[@name='wattage'])]

Simple indeed :P

Upvotes: 6

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

In case the current node is the parent element of the bean elements, one XPath expression that selects the wanted elements is:

bean[not(property/@name = 'wattage')]

This is probably the simplest such expression (has only a single predicate).

This expression translated in English says:

Select all bean children of the current node for which no name attribute of any of their property children is the string "wattage".

Upvotes: 2

G&#244;T&#244;
G&#244;T&#244;

Reputation: 8053

Try

//bean[not(property[@name='wattage'])]

Upvotes: 4

Related Questions