Understanding the Basics of XPath

XPath is a part of XSLT standard and as the name indicates it is used for the navigation in an XML document. XPath has a library of more than 100 built in functions for describing the different parts of XML document. It is based on path expressions for locating and positioning the elements of an XML file.

Let’s discuss the basic terms and concepts used in XPath. In XPath. XML document is viewed as a tree of nodes and different elements of document like attributes, text, comment and processing instruction are treated as nodes. For selecting nodes from the XML document, path expressions are used. Some examples of path expression are discussed in the following section.

First example of path expression is / which represent the root node. There is // which selects all the nodes from the document which satisfy the given criteria. Other examples are . and .. which select the current node and parent of current node respectively.

In addition to the locating elements in the XML file, XPath is capable of performing different types of mathematical and logical operations on these elements. Example of these operators are addition (+), subtraction (-), multiplication (*), division (div), less than (<), greater than (>), or, and, modulus (mod) etc.

Now let’s understand XPath from a practical example. Consider the following XML document:

<?xml version="1.0" encoding="ISO-8859-1"?>

<employeelist>

<employee1 >
<name>Todd Rody<name>
<age>37</age>
<gender>male</gender>
<DOJ>1997/03/24</DOJ>
</employee1>

<employee2 >
<name>Samuel Clark<name>
<age>46</age>
<gender>male</gender>
<DOJ>1993/09/12</DOJ>
</employee2>

</employeelist>

Now after loading this document in the browser you need to use selectNodes() method for selecting node from the XML document. Let’s discuss some example how to use XPath expression and operators.

For selecting the name of first employee:
/employeelist/employee[1]/name

For selecting age node with age>40:
/employeelist/employee[age>40]/age

For selecting name node with age>40:
/employeelist/employee[age>40]/name

For selecting text from DOJ node:
/employeelist/employee/DOJ/text()

In the last example text( ) function of XPath library is used. There are different categories of functions available in XPath like functions on numeric values, functions on strings, functions on Boolean values, functions on nodes, error functions, accessor functions, and functions on Sequences.

Some common examples of functions on numeric values are abs( ) which is used to return the absolute value of argument, number( ) which returns the numeric value of argument, ceiling( ) which returns the smallest number greater than the argument and floor() which returns the largest number not greater than the argument.

Similarly some commonly used examples of functions on strings are string( ) which returns the string value of argument, concat(str1, str2) which returns the concatenation of both the strings, string-length( str) which returns the length of the string.

So XPath provide a rich library with number of built-in functions dealing with different types of data types which make it more efficient and useful. Also other XML utilities like XQuery and Xpointer are built on the basis of XPath expressions.

| How to Parse an XML Document using DOM | Understanding Code Management in .NET | Understanding Enterprise Transaction Services in .NET | Understanding .NET Interface-Based Programming | Understanding Security Management in Web Services | Understanding SOAP Message and Delivery Structure | Understanding the Basics of Language Integrated Query (LINQ) | Understanding the Basics of Web Services using .NET | Understanding the Basics of XPath |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.