XPath Tutorial: How To Find Data in XML with Examples
XPath is used to locate and select data inside XML documents. It helps applications, parsers and tools find specific elements, attributes or text inside XML structures.
- XPath is a query language for XML.
- XPath expressions select elements, attributes and text.
- XPath uses paths similar to folders and files.
- XPath is commonly used in XML parsers, XSLT and automation tools.
What is XPath?
XPath stands for XML Path Language. It is used to navigate XML documents and select specific data.
XPath expressions can target:
- Elements
- Attributes
- Text values
- Nested structures
- Specific positions
XPath works a bit like GPS for XML. Instead of searching every cheese shelf manually, XPath can jump directly to the exact cheese you want.
Sample XML document
The examples below use this XML:
<company>
<employee id="101">
<name>Jane Smith</name>
<role>Chief Cheese Tester</role>
</employee>
<employee id="102">
<name>Mia Cheese</name>
<role>Bridge Snack Coordinator</role>
</employee>
</company>
Basic XPath syntax
XPath expressions look similar to file paths.
| XPath | What it selects |
|---|---|
/company |
The company root element. |
/company/employee |
All employee elements. |
/company/employee/name |
All employee names. |
//role |
All role elements anywhere in the document. |
Selecting XML elements
This XPath selects all employee names:
/company/employee/name
Result:
- Jane Smith
- Mia Cheese
What does // mean in XPath?
The double slash // searches the XML document recursively.
//role
This selects every role element anywhere inside the XML document.
XPath predicates
Predicates filter XPath results using square brackets.
/company/employee[1]
This selects the first employee element.
Second employee example
/company/employee[2]
This selects the second employee.
Selecting attributes with XPath
XPath uses the @ symbol for attributes.
/company/employee/@id
Result:
- 101
- 102
XPath filter example
XPath can filter results using conditions.
/company/employee[@id='101']
This selects the employee where the id attribute equals 101.
Selecting text with XPath
XPath can directly select text nodes.
/company/employee/name/text()
Result:
- Jane Smith
- Mia Cheese
Wildcards in XPath
The * wildcard selects any element.
/company/*
This selects all direct child elements inside company.
XPath and XML namespaces
XPath becomes more complex when XML namespaces are used.
<book:title
xmlns:book="https://cheesebridge.com/ns/book">
XML Adventures
</book:title>
XPath queries usually need namespace-aware prefixes when working with namespaced XML documents.
Common XPath use cases
- XML parsing
- XSLT transformations
- Web automation tools
- SOAP APIs
- Enterprise integrations
- XML validators and processors
XPath vs CSS selectors
| Feature | XPath | CSS Selectors |
|---|---|---|
| Designed for XML | Yes | Mainly HTML |
| Select attributes | Yes | Limited |
| Navigate upward | Yes | No |
| Complex filtering | Strong support | More limited |
Format or view XML online
Use CheeseBridge XML tools to inspect XML documents before testing XPath expressions.
Open XML Viewer Open XML FormatterTrusted references
For official and technical references, see:
Frequently asked questions
What is XPath used for?
XPath is used to find and select data inside XML documents.
What does // mean in XPath?
The double slash searches recursively through the XML document for matching elements.
Can XPath select XML attributes?
Yes. XPath uses the @ symbol to select attributes.