What is Xpath , Absolute XPath and Relative XPath?

What is Absolute XPath and Relative XPath




What is XPath ?

XPath (XML Path Language), as defined by the World Wide Web Consortium (W3C), is a query-language for selecting nodes from an XML document. In addition, XPath can be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. The following statements are important information regarding XPath:

XPath is a syntax for defining parts of an XML document
XPath uses path expressions to navigate in XML documents
XPath contains a library of standard functions
XPath is a major element in XSLT and in XQuery
XPath is a W3C recommendation


How to use Absolute XPath and Relative XPath?

Using Absolute XPath

Using Absolute XPath the user can directly access or find elements from the DOM. The main disadvantage of using Absolute XPath is that, if there are any changes made in the path of the element then that XPath fails to identify that particular element.

Absolute XPath begins with a single forward slash "/", which means you can select the element from the root.

Here is an example of Absolute XPath

/html/body/div[5]/div[2]/div/div[2]/div[2]/h2[1]


Using Relative XPath

Using Relative XPath the user can select an element from anywhere from the DOM…
Relative XPath starts with the double forward slashes ‘//’, which means it can search the element anywhere in the webpage.

Here is an example of Relative XPath 

.//*[@id='answers']/h2[1]/a[1]




Share this

Related Posts

Previous
Next Post »