tags:

views:

46

answers:

3

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 :)

+2  A: 

Okay, after a little digging i figured it out:

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

Simple indeed :P

Here Be Wolves
@Here-Be-Wolves: A minor problem: there is no element named `beans` in your provided XML...
Dimitre Novatchev
of course, I meant "bean". thanks for pointing out.
Here Be Wolves
+2  A: 

Try

//bean[not(property[@name='wattage'])]
GôTô
+1  A: 

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".

Dimitre Novatchev