tags:

views:

427

answers:

2

is it possible to find all nodes that don't have a specified child node?

for example:

(xml)

<item>
    <name>item 1</name>
    <admin>true</admin>
</item>

<item>
    <name>item 2</name>
    <admin>true</admin>
</item>

<item>
    <name>item 3</name>
    <parent>item 1</parent>
    <url></url>
    <admin>false</admin>
</item>

I want to pick out all nodes that don't have a child node "parent". I can do this if I set an attribute named parent and call:

(jquery)

$(xml).find("item:not([parent])").each

but I was wondering if this is possible by using a child node instead.

A: 

Could you use jQuery's native parent() selector?

easement
I'm actually trying to create a menu system with flyouts. I was trying to set it up without defining the hierarchy of the menus within the DOM of the xml file. Basically, if a menu is a flyout, it has a parent. It starts to get too complicated in the xml if I try to emulate the menu relationships within the DOM structure of the xml file (i.e.trying to select children of children, etc.) With that being the case, I won't be able to use the parent() selector since the actual hierarchy I want in the end isn't necessarily reflected in the DOM.
malificent
I was thinking also in doing it this way, I can reuse items if needed.
malificent
+1  A: 

You may get other good suggestions -- possibly based only on selectors -- but I believe this will work.

$('item').filter(function() {
    return $(this).find('parent').length === 0;
}).doSomethingWithTheSetOfItemsWithoutParents();

UPDATE

Based on selector documentation, I think this one will do what you want:

$('item:not(:has(parent))')
Drew Wills
thanks for the reply...I forgot to mention that I was able to get it to work with something similar to what you have here, but I was hoping there was a way to accomplish it with selectors.
malificent
ah...perfect! thanks!
malificent