Xpath expressions in Odoo 8

Hi guys,

In this tutorial I will learn you how to use xpath expressions. Xpath expressions give you the ability to add new pages, groups, fields, … to an already existing view. When you inherit a currently existing view in a module and want to extend it you will absolutely need xpath expressions. In this sample I will add new pages, groups and fields to the view under sales > products.

Tip:view my sample xpath module.

1. Create new fields

The first step is to create new fields inside your model. For example:

2. Inherit existing view

When you’ve created your fields you need to show them. In this example I will inherit the view from the product module which is showed under sales > products. You can reference another view with the ref=”” tag. The first step is to find the name of the view you want and then adding the module name in front of it. For example:

This now links to the product module to the view product_template_form_view (which you can find in product_view.xml under the product module). After we’ve inherited the view we can add the xpath expressions.

3. Xpath expressions

xpath expressions are basically paths to the page / group / field inside the view that you’ve inherited and are always built in the same way. Something along these lines:

Since the syntax is always a bit different I will simply give an example of each. The idea is to give the name from the page / group / field / .. so that Odoo can find the correct location to insert your new fields.

3.1 xpath page

This code will create a new page after the currently existing page named ‘Information’. The new page will be named ‘Custom page’ and will show the field FieldNewPage.
Which will result in the following page:
xpath page

3.2 xpath group

This code will create a new group after the group, inside the page with as title ‘Information’ and will show the field ‘FieldAfterGroup’ in it.
Which will result in the following page:
xpath group

3.3 xpath fields

This code will show the fields ‘CostPrice’ and ‘ShippingCost’ after the field ‘standard_price’. This is by far the easiest xpath expression and in usual cases this will work just fine.
This code will result in the following page:
xpath fields

Tip: there are three options for the position tag:

These are the most used xpath expressions but there are many more, as you can see in the source code of Odoo.
xpath is very powerfull and has a lot of options but sadly there is barely any documentation about it. I’ve created a sample module which you can view on my Github account.
Tutorial sponsored by Oocademy




22 thoughts on “Xpath expressions in Odoo 8

  1. Denis says:

    Hi, very helpful read on xpath expressions.

    Is it possible to use xpath expressions on smart buttons e.g. on the customer form? I would like to add a balance field as part of the smart buttons on the partner base.view_partner_form. Here is a sample of my code

    Balance
    res.partner

    • Yenthe666 says:

      Hi Denis,

      Thanks a lot! Yep, you can do xpaths on about anything in Odoo, including Smart Buttons. Just do an xpath on the button or the header, whatever works best for you!

  2. Martin Brehmer says:

    A very good article about the usage of XPath in odoo.

    But there is one thing that might lead to misunderstandings.
    XPath is not just a part of odoo. It is part of XML, so it is of course documented. Not by odoo, but by the World Wide Web Consortion (W3C).

    A good starting point to get deeper into the power of XPath is the w3school:

    http://www.w3schools.com/xsl/xpath_intro.asp

    • Yenthe666 says:

      Hi TungNT,

      Thank you! Sadly I can’t give you my Skype. I literally get dozens of requests like this a month and I really can’t keep up with the major load of questions.. Sorry, I hope you can understand that.

Leave a Reply