Bookmark and Share

XPath - Element

The most common usage of XPath is for selecting elements in an XML document. This lesson will provide a walkthrough of selecting many different elements, at different levels, in the XML Tree.

Advertise on

We will be using our lemonade2.xml file, which you can download. Below is the contents of lemonade2.

XML Code, lemonade2.xml:

		<lemonade supplier="mother" id="1">
		<pop supplier="store" id="2">

		<chips supplier="store" id="3">


XPath - A Path of Elements

When trying to reach a specific element in your XML document, you often have to string together many elements to get there. In XPath you reference an element by using its name. For example, the root element's name in lemonade2.xml is inventory, so we would type the following to reference it.

XPath Expression:


If we wanted to reference the drink element of inventory, we would have to string together these two elements, starting with the root element, inventory.

XPath Expression:


Notice: The elements are separated by a slash "/".

XPath - A Path of Children

If we wanted to select the price of pop, we would have to make an even longer path of elements to reach our final destination.

XPath Expression:


Notice: Each element added to this XPath expression is the child of the element before it.

  • drink is the child of inventory
  • pop is the child of drink
  • price is the child of pop

You can use this knowledge of parent-child relationships in an XPath expression to make sure your XPath is written properly.

Bookmark and Share

Download's XML Book

If you would rather download the PDF of this tutorial, check out our XML eBook from the store.

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials