Use this when selecting XML nodes with XPath expressions, predicates and namespaces.
XPath
XPath selects nodes in an XML document with path expressions, such as //interface[name='Gi1']/enabled. It is used in NETCONF filters, Select-Xml and lxml queries.
1. XPath selects nodes in XML.
2. Handle namespaces (prefix maps or local-name()).
3. Test queries on samples.
4. NETCONF filters use XPath or subtree filters.
5. Keep expressions readable.
XML tree + XPath → selected nodes
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- XPath without namespace prefixes returning nothing.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- XPath selects nodes with path expressions.
- Predicates in [] filter nodes.
- Namespaced elements need prefixes.
xmllint --xpath '//*[local-name()="interface"]/*[local-name()="name"]' file.xml— namespace-agnostic XPathpython3 -c 'from lxml import etree'— lxml XPath- Namespace map for queries — correctness
ReferenceWhy it matters1 section · depth on demand
What it is
XPath selects nodes in XML documents and is used in NETCONF filters.
ReferenceImportant terms & boundaries3 sections · depth on demand
In depth
XPath is a query language for selecting parts of an XML document: elements by path, by attribute values, by position or by content. It is used in NETCONF filters, XSLT, and XML libraries in Python and PowerShell.
| Expression | Selects |
|---|---|
/interfaces/interface | All interface elements under the root |
//interface[name='Gi1'] | Interface with that name anywhere |
//vlan/@id | All vlan id attributes |
//interface[enabled='true']/name | Names of enabled interfaces |
count(//interface) | Number of interfaces |
from lxml import etree
tree = etree.parse("config.xml")
ns = {"if": "urn:ietf:params:xml:ns:yang:ietf-interfaces"}
print(tree.xpath("//if:interface[if:enabled='false']/if:name/text()", namespaces=ns))
Example
root.xpath('//if:interface[if:name="Gi1"]/if:enabled', namespaces={'if': 'urn:ietf:params:xml:ns:yang:ietf-interfaces'})
Mechanics
The linked authority for bounded mechanics is W3C — Extensible Markup Language (XML) 1.0; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in XML > XPath. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.w3.org/TR/xml/ · confidence 94%