How to Select and Edit using Xpath in Odoo
XML views in Odoo can be customized using Xpath, by focusing on particular components like fields, buttons, and groups developers can change pre-existing Odoo views without having to duplicate the view design.
The two syntax elements that are frequently utilized in the XPath Tag are
//(Double Slash): Chooses every element that matches, no matter where it is in the XML views.
/(Single Slash): Chooses components at a certain path about the parent.
Practical Example: Adding a Discount Field in the Sale Order Line
Suppose you want to add a field named discount in the Order Lines section of the Sale Order Form view, right next to the price_unit field. The corresponding XML code would look like this:
<record id="inherit_sale_order_form_view" model="ir.ui.view">
<field name="name">inherit.sale.order.form.view</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="XML">
<xpath expr="//field[@name='order_line']/tree/field[@name='price_unit']" position="after">
<field name="discount"/>
</xpath>
</field>
</record>
To understand this Let us break down The XPath Expression mentioned in the code i.e
<xpath expr="//field[@name='order_line']/tree/field[@name='price_unit']"position=" after">
1. The expr Attribute
The expr keyword stands for “expression,” defining the XPath query used to locate the element.
2. Double Slash (//)
The expression //field[@name=’order_line’] searches through the entire XML structure of the Sale Order form view to find any field element where name=”order_line”.
The double slash (//) allows the search to go through nested or parent tags until it finds the correct element. It has to be noted that // only considers unique field names, If there are multiple fields with the same name then it won’t be properly executed.
3. Single Slash (/)
After locating the field [@name=’order_line’], the single slash (/) selects its immediate child elements.
In this case, /tree selects the tree tag because that’s an immediate child of the order_line field.
Example structure of order_line:
<field name="order_line">
<form>
...
</form>
<tree>
.....
<field name='price_unit'/>
</tree>
</field>
From the sample code it can be seen that the immediate child elements of the field [@name=’order_line’] are form and tree, Suppose we want to select the form then we should use /form after //field[@name=’order_line’]
4. Locating the price_unit Field
Within the tree tag, the next part /field[@name=’price_unit’] selects the specific field element where name=”price_unit”. The single slash(/)
is used because the immediate child element of the tree tag is <field name=’price_unit’/>. For reference,e you can see from the sample structure code of order_line that <field name=’price_unit’/> is the immediate child
of the tree tag
5. Position Attribute
Once the targeted element is located through expr, The next thing mentioned in the XPath tag is the position attribute, i.e. position=” after”. This ensures that the field named discount should be placed next to the field named price_unit.
Positioning Attributes in XPath
These are the possible position paths that can be given in the position attribute
position=”inside”: Adds content inside the targeted element.
position=”before”: Adds content before the targeted element.
position=”after”: Adds content after the targeted element.
position=”replace”: Replaces the targeted element entirely.
The provided XPath expression in this example effectively customizes the Sale Order form view by adding a discount field to the Order Lines tree view, next to the price_unit field. Understanding and using XPath syntax such as //, /, and position attributes allows developers to customize Odoo views without duplicating existing views.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote