XPath is a query language used to navigate and select nodes in XML documents. In Odoo, XPath is a core mechanism for view inheritance, allowing developers to modify or extend existing views without rewriting them entirely.
This article explains how XPath traversal works, how it is applied in Odoo XML views, and the correct way to use different XPath expressions and positions.
Understanding XPath Traversal
When we use / at the beginning of the expression, it will define an absolute path to the node relative to the root. It will find the node only at the root.
Consider the following XML structure:
<div class="node1">
<div class="node2">
<span class="node3">
<p class="node4">
<div class="node5"/>
</p>
</span>
</div>
</div>
Forward Traversing in XPath
Forward traversal moves from a parent node to its child or descendant nodes.
/node — Absolute Path
/div
- Starts traversal from the root element
- Selects only nodes directly under the root
In the example above, this selects:
<div class="node1"/>
/node — Relative Path Anywhere in the Document
//div
- Selects all <div> elements in the XML document
- One of the most commonly used XPath patterns in Odoo
/node1/node2 — Direct Parent–Child Relationship
/div/div
- / in the middle defines a direct child
- Selects <div class=”node2″ > from <div class=”node1″>
/node1//node2 — Any Descendant of a Parent
/div//div
- // allows traversal to any depth
- Selects all <div> elements inside <div class=”node1″>
//node1//node2 — Fully Relative Traversal
//div//div
- Selects from any <div>
- Selects any <div> inside it
Backward Traversing in XPath
XPath supports backward traversal using .. which represents the parent node.
Single-Level Backward Traversal
//div[@class='node5']/..
- Moves one level up
- Selects <p class=”node4″>
Multiple Levels Up
//div[@class='node5']/../..
- Moves two levels up
- Selects <span class=”node3″>
Each .. moves one level upward in the XML hierarchy.
XPath Usage in Odoo View Inheritance
In Odoo, XPath is used inside inherited views to locate and modify existing XML nodes.
<xpath expr="XPath expression" position="operation">
<!-- XML content -->
</xpath>
Examples:
<xpath expr="//field[@name='email']" position="attributes">
<attribute name="readonly">1</attribute>
</xpath>
XPath position Values in Odoo
inside
Adds XML inside the selected node.
<xpath expr="//group" position="inside">
<field name="x_note"/>
</xpath>
before
Inserts XML before the selected node.
<xpath expr="//field[@name='phone']" position="before">
<label for="phone"/>
</xpath>
after
Inserts XML after the selected node.
<xpath expr="//field[@name='phone']" position="after">
<field name="mobile"/>
</xpath>
replace
Replaces the selected node completely.
<xpath expr="//field[@name='email']" position="replace">
<field name="email" readonly="1"/>
</xpath>
attributes
Adds or modifies attributes on the selected node.
<xpath expr="//field[@name='email']" position="attributes">
<attribute name="required">1</attribute>
</xpath>
Common XPath Selectors in Odoo
Select fields by name
//field[@name='amount_total']
Select buttons by action
//button[@name='action_confirm']
Select elements by class
//div[hasclass('oe_button_box')]
The hasclass() function is useful when working with CSS classes in Odoo views.
Debugging XPath in Odoo
To understand how XPath applies to a view:
- Enable Developer Mode
- Open the form or view
- Click Edit View
- Inspect the compiled XML structure
- Write XPath expressions based on the final XML
Conclusion
XPath is a powerful and flexible way to navigate XML structures in Odoo. By understanding forward and backward traversal, relative and absolute paths, and the available XPath positions, developers can confidently extend and customize Odoo views.
Clear and readable XPath expressions make view inheritance easier to maintain, debug, and extend as requirements evolve.
That’s it!
You’re all set. Happy coding!
Current Product Version - 1.0.0
Supported Framework Version - xml

4 comments