Javatpoint Logo
Javatpoint Logo

XPath Expression

XPath defines a pattern or path expression to select nodes or node sets in an XML document. These patterns are used by XSLT to perform transformations. The path expressions look like very similar to the general expressions we used in traditional file system.

Xpath Expression 1

XPath specifies seven types of nodes that can be output of the execution of the XPath expression.

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

We know that XPath uses a path expression to select node or a list of nodes from an XML document.

A list of useful paths and expression to select any node/ list of nodes from an XML document:

Index Expression Description
1) node-name It is used to select all nodes with the given name "nodename"
2) / It specifies that selection starts from the root node.
3) // It specifies that selection starts from the current node that match the selection.
4) . Select the current node.
5) .. Select the parent of the current node.
6) @ Selects attributes.
7) student Example - selects all nodes with the name "student".
8) class/student Example - selects all student elements that are children of class
9) //student Selects all student elements no matter where they are in the document

XPath Expression Example

Let's take an example to see the usage of XPath expression. Here, we use an xml file "employee.xml" and a stylesheet for that xml file named "employee.xsl". The XSL file uses the XPath expressions under select attribute of various XSL tags to fetchvalues of id, firstname, lastname, nickname andsalary of each employee node.

Employee.xml

Employee.xsl

Output:

Xpath Expression
Next TopicXPath Nodes




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA