Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - xpath find?

Tags:

jquery

xml

xpath

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>
        ...
like image 943
Steve Avatar asked Apr 26 '10 20:04

Steve


2 Answers

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

like image 124
Fyodor Soikin Avatar answered Sep 20 '22 11:09

Fyodor Soikin


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.

EDIT: Updated link to xpath plugin XPath Plugin

like image 43
Matthew Flaschen Avatar answered Sep 22 '22 11:09

Matthew Flaschen