Tags

,


What is XPath?

The syntax for defining parts of XML doc and also used to navigate in XML doc. It has a library of standard fun also a major part of XSLT.

XPath Expression:

Expressions are very much like file system and used to select node or node-set in a XML doc.

XPath Terminology:

In XPath there’re seven kinds of nodes. Nodes are treated as tree.

  • Root: Document node the Root node.
  • Atomic Node: node with no children or parent.
  • Items: Atomic values or Node
Node Description Example
  • Document(Root)
Here BOOKSTORE is the document node <?xml version=”1.0″ encoding=”ISO-8859-1″?>

<bookstore>

<book>
<title lang=”en”>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

</bookstore>

  • Element
The BOOK, Author is element node. Author is the child node of BOOK node <bookstore> (document node)

<author>J K. Rowling</author> (element node)

lang=”en” (attribute node)

  • Attribute
Modifies some element/node <author Lang=”en”, country=”bn”></author>
  • Text
  • Processing Instruction
  • Comment

Relationships of Node:

Relationship may be viewed like following:

We may describe the relation by following terms:

Relation Description Example
Parent book element is the parent of the title, author, year, and price <book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
Children the title, author, year, and price elements are all children of the book element the title, author, year, and price elements are all children of the book element:
Siblings Nodes that have the same parent. the title, author, year, and price elements are all siblings
Descendants A node’s children, children’s children, etc. descendants of the bookstore element are the book, title, author, year, and price elements

XPath Syntax

  • Example XML
  • Selecting Nodes by Path Expression

XPath uses path expression to select nodes in XML. Most used paths are below:

Expression Description
nodeName Selects all child node of the named node
/ Selects from the root node
// Selects nodes in the documents from the current node that match the selection no matter where they are
. Selects the current node
.. Select the parent of the current node
@ Selects attributes
  • Examples of Path Extension and Results
Path Expression Description
Bookstore Select all child node of the bookstore element
/bookstore Set bookstore is the root or “Select the root element bookstore” (absolute Path)
/bookstore/book Select all the book that are children of bookstore
//book Select all the book elements no matter where they are in the document
/bookstore//book Select all book elements that are “DECESDENT” of book store element. No matter whether they are under bookstore element.
//@lang Selects all the attributes that are named lang
  • Predicates

Predicates are used to find:

A Specific node that contains a specific value

Always embedded in square brackets

  • Examples of Predicates Expression
Predicates Path Expression Description
/bookstore/book[1] Select the first book element that is the child of the bookstore element
/bookstore/book[last()] Selects last book element that is the child of the bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3] Select first TWO book element that are child of bookstore
/bookstore//book Select all book elements that are “DECESDENT” of book store element. No matter whether they are under bookstore element.
//title[@lang] Select all title element that have an attribute named lang
//title[@lang=’eng’] Select all the TITLE element that have an attribute named lang with value “eng
/bookstore/book[price>35] Select all book element that have a price element with value greater than 35
/bookstore/book[price>35]/title Select all the book title element that have a price greater than 35
  • Selecting Node with Wildcard
WILDCARD Description
* Matches any element node (other than the DOCUMENT(ROOT) node)
@* Matches any attribute node
Node() Matches any Node of any kind (Including document node)
  • Selecting Node with Wildcard
WILDCARD Expression (Path Expression) Description
/bookstore/* Select all the Child of the bookstore element
//* Select all element in the file(xml) including ROOT
//title[@*] Select all title element which have any attribute
  • XPath Axis

An Axis defines a node-set relative to the current node. In other word, AXIS defined a tree-relationship between selected node and current node.

Location of Path Expression

An absolute location path:

/step/step/…

A relative location path:

step/step/…

WILDCARD Expression (Path Expression) Description
Ancestor Selects all ancestor of the current node
Ancestor-or-self Select all ancestor of the current node, including current node
Attribute Select all the attributes of the current node
Child Selects all children of the current node
Descendent Selects all the Descendent of the current node
Following Selects everything in the document after closing node of the current node
Following-Sibling Selects all sibling after the current node
Namespace Selects all namespace node of the current node
Parent Selects the parent of the current node
Preceding Selects everything in the document before the current node
Self Select the current node

Example of AXIS Expression:

Example Result
Child::* Select all children of the current node
Child::book Select all book nodes that are children of current node
Attribute::* Select all attribute of the current node
Attribute::lang Select the lang attribute of the current node
Child::text() Select all text child nodes of the current node
Child::node() Select all child nodes of the current node
Descendent:: book Select all book descendent of the current node
Ancestor::book Select all book ancestor of the current node
Ancestor-or-self::book Select all book ancestors of the current node and the current as it is a book node.
Child::*/Child:: price Select all price grandchild of the current node

XPATH Operators:

Example Example
| //book|//cd >> Return a node-set of books or cds
+, -, *, div, =,! =, ….. As Known >> Price!=100
AND, OR Price > 100 AND price< 200
MOD 5 mod 2

Development Notes:

Selecting Nodes

Unfortunately, there are different ways of dealing with XML and XPath in Internet Explorer and other browsers.

In our examples we have included code that should work with most major browsers.

Select nodes for Internet Explorer

Using the Microsoft XMLDOM object to load the XML document and the selectNodes() method to select nodes from the XML document:

xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.load(“books.xml”);

xmlDoc.selectNodes(xpath);

Select nodes for Firefox and Opera

Using the implementation() method of the document object to load the XML document and the evaluate() method to select nodes from the XML document:

xmlDoc=document.implementation.createDocument(“”,””,null);
xmlDoc.async=false;
xmlDoc.load(“books.xml”);

xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null);

Advertisements