XPATH Examples

XPATH Examples


R4R provide basic XPATH Tutorials concept with XPATH Examples .Through R4R you can develop XPATH programming concept. R4R provide XPATH Interview Questions with answers.R4R provide XPATH Languages study materials in easy way.

Introduction of XPath

XPath is the solution to finding information in an XML document. XPath uses expressions to find elements, attributes, and other information in your XML.

XML documents have a tree structure

  1. XPath allows you to concisely describe sets of XML elements and attributes in this tree.
  2. Analogy to file system paths.
  3. A file system hierarchy is a tree of directories and files.
  4. A filesystem path describes how to reach a specific file, starting either from the root, or the current working directory.
  5. XPath exploits this intuitive notion of a path, and extends it to XML documents.
Nodes of XPath

In the XML tree available for XPath expressions, there are 7 node types

  1. The root node (one per document)
  2. Element nodes
  3. Attribute nodes
  4. Text nodes
  5. Comment nodes
  6. Processing instruction nodes
  7. Namespace nodes
Simple Example

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


  <title lang="eng">Harry Potter</title>

  <title lang="eng">Learning XML</title>


Some Expression How to Locate path
Expression Description
/Selects from the root node
//Selects nodes in the document from the current node that match the selection no matter where they are
.Selects the current node
..Selects the parent of the current node
@Selects attributes
Path Expression Result
/bookstoreSelects the root element bookstore
bookstore/bookSelects all book elements that are children of bookstore
//bookSelects all book elements no matter where they are in the document
bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
//@langSelects all attributes that are named lang
Advantage of XPath
  1. Flexible expression- The user may use whatever tags/attributes seem intuitive when designing the schema
  2. latent semantics- Many users prefer to figure out the semantics when/if necessary
  3. Application Specific Constraint Checking
  4. Core Technology- XSLT/XPath is a "core technology" which is well supported, well understood, and with lots of material written on it.
  5. Expressive Power- XSLT/XPath is a very powerful language. Most, if not every, constraint that you might ever need to express can be expressed using 6.XSLT/XPath. Thus you don't have to learn multiple schema languages to express your additional constraints
  6. Long Term Support- XSLT/XPath is well supported, and will be around for a long time.
Disadvantage of XPath
  1. Separate Documents- With this approach you will write your XML Schema document, then you will write a separate XSLT/XPath document to express additional constraints. Keeping the two documents in synch needs to be carefully managed
  2. Efficiency is access limited
XPATH Tutorials
XPATH Examples
XPATH Interview Materials