XPath
Course Index
Index

XPath Cheat Sheet

Types of nodes



  • Root
  • Element
  • Text
  • Attribute
  • Comment
  • Processing Instruction
  • Namespace

Example XML



XML
Copy
<?xml version="1.0" encoding="UTF-8"?>
<employees>
	<employee type="payrol" status="permanent">
		<name>John Doe</name>
		<age>28</age>
		<salary variable_percent="7">6000</salary>
	</employee>
	<employee type="contract" status="na">
		<name>Michal lee</name>
		<age>42</age>
		<salary variable_percent="0">5000</salary>
	</employee>
	<employee type="payrol" status="probation ">
		<name>Aliice Richards</name>
		<age>24</age>
		<salary variable_percent="0">4000</salary>
	</employee>
</employees>

Selecting Nodes



To select all the nodes with the given name "node-name". node-name

To select from root node. /

To select from the current node that match the selection. //

To select the current node. .

To select the parent node of current node. ..

To select the attribute. @

To select all nodes with name "employee". employee

To select the root element employees. /employees

To select all employee that are children of employees. employees/employee

To select all employee elements no matter where they are in the document. //employee

To select all employee elements that are descendant of the employees element. employees//employee

To select all attributes named "type". //@type

Predicates



Predicates are utilized to locate a specific node or one that holds a particular value. They are always enclosed within square brackets.

To select the first employee element that is the child of the employees element. /employees/employee[1]

To select the last employee element that is the child of the employees element. /employees/employee[last()]

To select the second last employee element that is the child of the employees element. /employees/employee[last()-1]

To select the given number of employee elements that are children of the employees element. /employees/employee[position()<2]

To select all the employee elements that have an attribute named type. //employee[@type]

To select all the employee elements that have a type attribute with a value of "contract". //employee[@type='contract']

To select all the employee elements that have a type attribute with a value of "contract" and status attribute with a value of "permanent". //employee[@type='contract' and @status='permanent']

To select all the employee elements that have a type attribute with a value of "contract" or "payrol". //employee[@type='contract' or @type='payrol']

To select all the employee elements of the employees element that have a age element with a value greater than 40. /employees/employee[age>40]

To select all the name elements of the employee elements of the employees element that have a age element with a value greater than 40. /employees/employee[age>40]/name

To select all the child element nodes of the employees element. /employees/*

To select all elements in the document //*

To selects all employee elements which have at least one attribute of any kind. //employee[@*]

To selects all the name AND age elements of all employee elements. //employee/name | //employee/age

XPath Axes



An axis defines a relationship to the context (current) node and is used to find nodes in relation to that node within the tree.


To select all ancestors of the current node. Example, parent, grandparent, etc. //name/ancestor::employee

To select all ancestors of the current node and the current node itself. Example, parent, grandparent, etc. //name[text()='John Doe']/ancestor-or-self::employee

To select all attributes of the current node. //employee/attribute::type

To selects all children of the current node. //employees/child::employee

To select all element children of the current node. child::*

To select all text node children of the current node. //employee/name/child::text()

To select all children of the current node. child::node()

To select all age grandchildren of the current node. //employees/child::*/child::age

Selects all descendants of the current node. Example children, grandchildren, etc. descendant::employee

To select all descendants of the current node and the current node itself. Example, children, grandchildren, etc. descendant-or-self::employee

To select all siblings after the current node. //employee[@status='permanent']/following-sibling::employee[1]

To select the parent of the current node. //employee/age[text()='28']/parent::node()

To select all siblings before the current node. //employee[@status='na']/preceding-sibling::employee[1]

To select the current node. //employee[@status='na']/self::node()

To select all attributes of the current node. //employee /attribute::*

Miscellaneous



To select the textual value of name element of first employee element. //employee[1]/name/text()

To select the name of the first element under employees. name(//employees/*[1])

To select the numeric value of the age element of the first employee element. number(//employees/*[1]/age)

To select the string representation value of the status attribute of the first employee element. string(//employees/*[1]/@status)

To select the length of the name element's textual value of first employee element. string-length(//employees/*[1]/name/text())

To select the local name of the first employee element, i.e. without the namespace. local-name(//employees/*[1])

To select the number of employee elements. count(//employee)

To select the sum of the text of salary elements of all employee elements. sum(//employee/salary)

To select the employee not having attribute value "contract". //employee[not(@type='contract')]

To select the employee contains attribute value "cont". //employee[contains(@type,'cont')]

To select the employee not contains attribute value "cont". //employee[not(contains(@type,'cont'))]

To get the 7% of salary of first employee. (//employee[@type = 'payrol'][1]/salary/text()*0.07)

Node functions



  • text()
  • count()
  • name()
  • position()
  • lang(str)

String functions



  • contains()
  • concat(x,y)
  • string-length()
  • substring(str, start, len)
  • substring-before("pattern", "/")
  • substring-after("pattern", "/")
  • starts-with()
  • ends-with()
  • translate()
  • normalize-space()