Free since 2005 · No login required
AT

Academic Tutorials

Learn at your own pace

site-mobile-top-banner · 320x50

Introduction to Xpath

Added 26 Jul 2008


According to the W3C, XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer. In this article we will learn about XPath, XPath expressions and how to use XPath in .NET and Java.

XPath, XML Path Language, is an expression language for addressing portions of an XML document or navigating within an XML document. We can also get values of elements and attributes using XPath. XPath is really helpful for parsing XML- based configuration or properties files. XPath is a major element in the XSLT standard.

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like URL and traditional file system paths. XPath also supports several functions for string manipulation, comparison and others.

First we will learn about these path expressions. XML documents are treated as trees of nodes and the root of the tree is called the document node or root node. There are about seven different kinds of nodes. They are element, attribute, text, namespace, processing-instruction, comment, and root nodes. Usually the most used nodes are element, attribute and text. Here is an XML sample.


 

   
XPath Tutorial
    
Mamun Zaman
   
2007
   
00.99
 

 

   
AJAX Tutorial
   
Charles
   
2007
   
03.45
 

In this XML sample, the root node is . We have two elements; these are siblings. The  node is a child of the <book> element. Similarly, the <book> node is a parent of <title>, <author>, <year> and <price>. The <book> and <bookstore> nodes are ancestors of the <price> node. Similarly, the <book>, <title>, <author>, <year> and <price> nodes are descendants of <bookstore>, the root node. The id is an attribute node. The year value 2007 is a text node.</p></span></div> </article> <div class="hidden md:flex my-10"> <div class="adsclk-slot " data-adsclk-zone="in-article" data-adsclk-w="336" data-adsclk-h="280" style="width:336px;height:280px;margin:0 auto">in-article · 336x280</div> </div> </main> <aside class="hidden lg:block"> <div class="sticky top-[80px] space-y-5"> <div class="adsclk-slot " data-adsclk-zone="content-sidebar-rectangle" data-adsclk-w="300" data-adsclk-h="250" style="width:300px;height:250px;margin:0 auto">content-sidebar-rectangle · 300x250</div> <div class="adsclk-slot " data-adsclk-zone="half-page" data-adsclk-w="300" data-adsclk-h="600" style="width:300px;height:600px;margin:0 auto">half-page · 300x600</div> </div> </aside> </div> </div> </main> <div class="max-w-[1440px] mx-auto px-4"> <div class="hidden md:flex my-10"> <div class="adsclk-slot " data-adsclk-zone="site-footer-leaderboard" data-adsclk-w="728" data-adsclk-h="90" style="width:728px;height:90px;margin:0 auto">site-footer-leaderboard · 728x90</div> </div> </div> <footer class="border-t border-slate-200 bg-slate-50"> <div class="max-w-[1440px] mx-auto px-4 py-10 grid md:grid-cols-4 gap-8 text-sm"> <div class="md:col-span-2"> <div class="flex items-center gap-2.5"> <span class="inline-grid place-items-center w-9 h-9 rounded bg-leaf-500 text-white font-bold">AT</span> <span class="font-semibold text-lg">Academic Tutorials</span> </div> <p class="mt-3 text-ink-700 max-w-md"> 138 tutorials · 616 articles · 1,126 interview Q&A · 991 FAQs · 1,080 books. Free since 2005. </p> </div> <div> <p class="font-semibold text-ink-900 mb-3">Learn</p> <ul class="space-y-2 text-ink-700"> <li><a href="https://academictutorials.com" class="hover:text-leaf-700">Tutorials</a></li> <li><a href="https://academictutorials.com/articles" class="hover:text-leaf-700">Articles</a></li> <li><a href="https://academictutorials.com/interview-questions" class="hover:text-leaf-700">Interview Q&A</a></li> <li><a href="https://academictutorials.com/faqs" class="hover:text-leaf-700">FAQs</a></li> <li><a href="https://academictutorials.com/books" class="hover:text-leaf-700">Books</a></li> </ul> </div> <div> <p class="font-semibold text-ink-900 mb-3">About</p> <ul class="space-y-2 text-ink-700"> <li><a href="https://academictutorials.com/about" class="hover:text-leaf-700">About us</a></li> <li><a href="https://academictutorials.com/contact" class="hover:text-leaf-700">Contact</a></li> <li><a href="https://academictutorials.com/privacy" class="hover:text-leaf-700">Privacy</a></li> <li><a href="https://academictutorials.com/sitemap.xml" class="hover:text-leaf-700">Sitemap</a></li> </ul> </div> </div> <div class="border-t border-slate-200"> <p class="max-w-[1440px] mx-auto px-4 py-4 text-xs text-ink-500"> © 2026 Academic Tutorials · A Phoenix Softwares property. </p> </div> </footer> <script src="https://banners.adsclk.com/loader.js" async defer></script> </body> </html>