XPath Tutorial

https://www.w3schools.com/xml/xpath_intro.asp

XPath is a major element in the XSLT standard.

XPath can be used to navigate through elements and attributes in an XML document.

  • XPath stands for XML Path Language
  • XPath uses “path like” syntax to identify and navigate nodes in an XML document
  • XPath contains over 200 built-in functions
  • XPath is a major element in the XSLT standard
  • XPath is a W3C recommendation
1
2
3
4
5
6
//tag[@attribute="value"]
<!--example-->
//div[@class="price"]
<!--list of elements-->
(//div[@class="price"])[1]
(//div[@class="price"])[2]  <!-- and so on... -->

Absolute XPath: Complete Path from Root Element

Relative XPath: You choose Element you want to start from (Use // if you choose Relative XPath)

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<html>
  <head>...</head>
  <body>
    <div class="head_wrap">
      <div class="head_bg">...</div>
      <div class="sidebar">...</div>
      <div class="head_menu">
        <div class="categories">...</div>
        <div class="main_menu">
          <div class="li_menu">...</div>
          <div class="li_menu">
            <a href="/goodchoice/">Good choice</a>
          </div>
        </div>  
      </div>
    </div>
  </body>
</html>  

Relative XPath:

1
//div[@class="main_menu"]/div[2]/a

Absolute XPath:

1
html/body/div/div[3]/div[2]/div[2]/a