tags:

views:

110

answers:

2

If you have the xml below in $(xml), you would get droopy using:

$(xml).find("animal").find("dog").find("beagle").text()

Is there an equivalent way in jQuery to use xpath like $(xml).xpathfind("/animal/dog/beagle").text()?

<animal>
    <dog>
        <beagle>
            droopy
        </beagle>
        ...
+1  A: 

jQuery used to support very basic XPath, including the example you gave.

$(xml).find("animal/dog/beagle")

EDIT: You're right, they've apparently removed it from the core, so you have to use a "compatibility" plugin.

Matthew Flaschen
I tried it, but it didn't work. The CSS selector syntax, "animal > dog > beagle" did. I tried "/a/d/b", "//a/d/b" too. Do I have to include a plugin?
Steve
I think it has to do with the leading slash. Try "a/d/b" instead of "/a/d/b"
Fyodor Soikin
Yeah, I tried an old xpath plugin from Resig, but looking at the source, it was minimal coverage of xpath. Oh well, the CSS selector will do. Just wanted to make sure there if xpath was part of the latest jQuery.
Steve
+1  A: 

jQuery supports basic XPath actually, so you can just use find.

Alternatively, use CSS selector syntax. For your particular example, you would use $(xml).find( "animal > dog > beagle" ).text()

Fyodor Soikin