XPath More examples

Attributes

//item[@price > 2*@discount]

Finds <item> and check its attributes

Closest

./ancestor-or-self::[@class="box"]

Works like jQuery's $().closest('.box').

Find a parent

//section[h1[@id='section-name']]

Finds a <section> that directly contains h1#section-name

//section[//h1[@id='section-name']]

Finds a <section> that contains h1#section-name. (Same as above, but uses descendant-or-self instead of child)

Examples

//*                 # all elements
count(//*)          # count all elements
(//h1)[1]/text()    # text of the first h1 heading
//li[span]          # find a <li> with an <span> inside it
                    # ...expands to //li[child::span]
//ul/li/..          # use .. to select a parent
Comments