4.5. Locating elements with XPath axis

Axis

Description

Example

Result

ancestor

Selects all ancestors (parent, grandparent, and so on) of the current node.

//td[text()='John']/ancestor::table

This will get the table element.

descendant

Selects all descendants (children, grandchildren, and so on) of the current node.

//table[@id='table1']/descendant::td[text()='JoJason']

This will get the element from the second column of the third row from the table.

following-sibling

Selects all siblings after the current node.

//td[text()='Jason']/following-sibling::td[1]

his will get the third column from the third row immediately after the column that has Jason as the text value.

preceding

Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes, and namespace nodes.

//td[text()='John']/preceding::tr

This will get the header row.

preceding-sibling

Selects all siblings before the current node.

.//td[text()='Smith']/preceding-sibling::td

This will get the first column of third row from the table.

Last updated

Was this helpful?