XPath Tutorial
Contents
1 What is XPath?
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
2 XPath Syntax
//tag[@attribute="value"]
<!--example-->
//div[@class="price"]
<!--list of elements-->
(//div[@class="price"])[1]
(//div[@class="price"])[2] <!-- and so on... -->
3 Absolute and relative XPath
Absolute XPath: Complete Path from Root Element
Relative XPath: You choose Element you want to start from (Use // if you choose Relative XPath)
Example:
<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:
//div[@class="main_menu"]/div[2]/a
Absolute XPath:
html/body/div/div[3]/div[2]/div[2]/a