Working with XML Lesson 5 of 9
XPath — Finding Data in XML
Navigate and search XML documents with XPath — paths, // searches, attributes with @, predicates in square brackets, and functions like count() and contains() — with a live XPath tester.
XPath is a small language for selecting parts of an XML document, like a file path for elements. It's used in XSLT, in programming languages, in web scraping tools, and even in some spreadsheet functions.
The sample document
<store>
<product id="P01" category="bread"><name>Pandesal</name><price>25</price><stock>40</stock></product>
<product id="P02" category="drinks"><name>Kape</name><price>8</price><stock>200</stock></product>
<product id="P03" category="drinks"><name>Softdrink</name><price>75</price><stock>24</stock></product>
<product id="P04" category="snacks"><name>Chips</name><price>15</price><stock>0</stock></product>
</store>
Path expressions
| XPath | Selects |
|---|---|
/store/product | All product elements under the root store |
//name | All name elements anywhere |
/store/product/@id | The id attribute of every product |
//product[1] | The first product (XPath counts from 1) |
//product[last()] | The last product |
//product[@category='drinks'] | Products whose category is drinks |
//product[price > 20]/name | Names of products costing more than 20 |
//product[stock = 0]/name | Names of out-of-stock products |
/at the start means "from the root";//means "anywhere below".@selects an attribute.- Square brackets
[ ]are predicates: conditions that filter the selection.
Try XPath live
<style>body{font-family:sans-serif} input{width:100%;padding:6px;font:15px monospace;box-sizing:border-box} pre{background:#f3f6f4;padding:8px}</style>
<p><input id="xp" value="//product[price > 20]/name"></p>
<button onclick="run()">Run XPath</button>
<pre id="out"></pre>
<script>
const xml = `<store>
<product id="P01" category="bread"><name>Pandesal</name><price>25</price><stock>40</stock></product>
<product id="P02" category="drinks"><name>Kape</name><price>8</price><stock>200</stock></product>
<product id="P03" category="drinks"><name>Softdrink</name><price>75</price><stock>24</stock></product>
<product id="P04" category="snacks"><name>Chips</name><price>15</price><stock>0</stock></product>
</store>`;
const doc = new DOMParser().parseFromString(xml, "application/xml");
function run() {
const out = document.getElementById("out");
try {
const res = doc.evaluate(document.getElementById("xp").value, doc, null, XPathResult.ANY_TYPE, null);
if (res.resultType === XPathResult.NUMBER_TYPE) return (out.textContent = res.numberValue);
if (res.resultType === XPathResult.STRING_TYPE) return (out.textContent = res.stringValue);
if (res.resultType === XPathResult.BOOLEAN_TYPE) return (out.textContent = res.booleanValue);
const lines = [];
let node;
while ((node = res.iterateNext())) lines.push(node.nodeType === 2 ? node.name + "=" + node.value : node.textContent.trim());
out.textContent = lines.length ? lines.join("\n") : "(no matches)";
} catch (e) {
out.textContent = "Invalid XPath: " + e.message;
}
}
run();
</script>
Try these: //product/@id, //product[@category='drinks']/name, count(//product), sum(//price), //product[stock=0]/name, //name[contains(., 'a')].
Useful functions
| Function | Example | Result |
|---|---|---|
count() | count(//product) | 4 |
sum() | sum(//stock) | 264 |
contains() | //name[contains(., 'K')] | Kape |
starts-with() | //product[starts-with(@id, 'P0')] | All four |
text() | //name/text() | The text nodes of names |
and, or | //product[price < 20 and stock > 0] | Kape |
The dot . means "the current element", as in contains(., 'K').
Where XPath is used
- XSLT templates (next lessons).
- Programming:
document.evaluatein JavaScript,lxmlin Python,DOMXPathin PHP,XPathclasses in Java and C#. - Web scraping and automated testing tools like Selenium and Playwright.
- Google Sheets'
IMPORTXML(url, xpath)function, which pulls data from web pages into a sheet.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04