Google News
logo
XPath Interview Questions
XPath stands for XML Path Language. It is defined as a query language for selecting nodes from an XML document. It is used to find values such as strings, numbers, or Boolean values from the content of an XML document. It is generally used to navigate through elements and attributes in an XML document. It adopts path like syntax to identify and navigate nodes in an XML document. It uses syntax for defining parts of an XML document It contains a library of standard functions. It follows the W3C(World Wide Web Consortium) recommendation.
XPath Path Expressions are a combination of variables and operators. To select nodes or node-sets in an XML document XPath uses path expressions that are used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages. It looks like the expressions with a traditional computer file system.
XPath stands for is used to find the location of any element on a webpage or to navigate through elements and attributes in an XML document.
XPath is a major element in the XSLT standard.

* It is syntax for defining parts of an XML document.
* XPATH stands for XML Path Language.
* It is used for navigating in XML documents.
* It can be used to query data from XML documents.
* It uses path expressions to navigate in XML documents.
* XPATH expressions are case sensitive.
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. 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
There are two types of location paths used to specify the location of node in XML documents. These paths are absolute or relative path.
An absolute path starts with root node or with '/'.
 
For Example :
 
/company/employee - It will select employee nodes within class root node.
An atomic value is a value in or an instance in the value space of an atomic type defined by XML Schema. Atomic values are consists of strings, integers, decimals, dates, etc. and these values are known as atomic because it cannot be subdivided.
Xpath Node is defined as a point where the path address initiates, as it follows a concept of nodes. Basically, it is an individual element of the Xpath hierarchical structure which is termed as a node and enables an XSL processing.
XPath uses operators on nodes such that it returns either a node-set, a string, a Boolean, or a number. Some of the examples are addition, subtraction, Multiplication, etc.
Using XPath-Multiple, we can write the Java code along with the dynamic XPath location as : findElement(By. xpath("//*[@id=' lst-ib'][@class=' gsfi lst-d-f']"));
As we know that path defines the location of a node using absolute or relative path. In the same manner XPath axes are used to identify elements by their relationship like parent, child, sibling, etc. Axes refer to axis on which elements are lying relative to an element.
XPath comparison operator is used to compare one value to another. There are different type of comparison operators :
 
* =
* !=
* <
* >
* <=
* >=

Boolean operators are simple words AND, OR, NOT etc. used to combine or exclude keywords. They connect your search words together to either concise or expand your set of results.
In XPath, number functions are used to fetch the different type of values from the expressions. For example, ceiling value, floor value etc.
 
There are four types of number functions in XPath :
 
* ceiling()
* floor()
* round()
* sum()
There are following types of nodes in XPath. These nodes are related to each other to form a tree like structure. These nodes are :
 
* Parents
* Children
* Siblings
* Ancestors
* Descendents

For example :
<book>  
  <title>Parallel Algorithm</title>  
  <author>Sri Balaji</author>  
  <year>2016</year>  
  <price>180.00</price>  
</book>

 

XPath has more than 101 build-in functions. These built-in functions are used in Boolean values, string values, numeric values, date and time comparison, sequence manipulation etc.
XPath axes are used to set relation of current node with another node. A list of XPath axes to set relation with other nodes are :
 
ancestor : It is used to get all ancestor (parent, grandparent etc) of current node.

ancestor-or-self : It is used to get all ancestors (parent, grandparent, etc.) of the current node and also the current node.

attribute : It is used to get all attributes of the current node.

child : It is used to retrieve all children of the current node.

descendant : It is used to get all descendants (children, grandchildren, etc.) of the current node.

namespace : It is used to get all namespace nodes of the current node.

preceding : It is used to get everything in the document that is comes before the start tag of the current node.

You can compare two values in Xpath with the help of the following steps :
 
* Create the path to the first node set that you want to compare.

* Then, type = (equal to), != (not equal to), > (greater than), >= (greater than or equal to), < (less than), or <= (less than or equal to), depending on how you want to compare the two values.

* Finally, type a value or a path to the node-set that you want to compare with the first node set identified in Step 1.
The preceding-sibling axis indicates all the nodes that have the same parent as the context node such that they appear before the context node in the source document.
The normalize-space function strips white-space from a string to replace sequences of whitespace characters with a single space and return the resulting string.
* The top-level elements don't have any effect on the behaviour of XSLT elements.
* The functions that are defined with it also don't have any affect in the document.
* The top-level elements are not permitted to be used by specifying the xsl:apply templates.
* XSLT process is provided to ignore the top-level elements and ignore some of it if an error occurs.
* XSLT top-element element doesn't recognize the namespace URI and can be provided using other sources.
The XPath string functions specify some rules which are used to get strings according to your preferences.
An XPath query allows you to select nodes from a document by applying expressions. For example, you can ask for all of the elements with a “text” tag and then filter by opening tags.
 
An XPath query is more complex than an XPath location path because it requires some form of logic or searches to identify what nodes are desired. Queries use full expressions that result in boolean values – true/false – which return nodes when the condition is satisfied.
There are five types of operators :
 
Arithmetic Operators : These include + (addition), – (subtraction), and *(multiplication).

Comparison Operators : These include =, >, >=, <=.

Logical Operators : these generally occur in a conditional statement such as “if” or “else.” They also return boolean values.

Text Operators : these operators are used to return text values, such as “text(), substring()”.

XPath Functions : These allow you to apply a function to the nodes that meet certain criteria. For example: length().
Advertisement