Ensuring that the modifications we make to the codes remain functional is necessary for our sites to continue operating in the best way, that's why we want to talk to you a little about XPath and how you can use it in your Odoo data base.
Is XPath new to you? We'll explain a little.
What is XPath?
XPath is an expression language that supports querying or transforming documents in XML. It's a path description language for XML documents developed by the W3C that allows finding specific elements in an XML document, without being based on that type of document.
XPath:
XML Path Lenguage.
XML:
Extensible Markup Lenguage.
W3C:
World Wide Web Consortium.
Do you know how it works?
This routing language is based on a model that interprets your XML document as a sequence of elements ordered in a tree structure, similar to the DOM (Document Object Model) that works as an interface between HTML and JavaScript in the web browser.
How to locate XML elements?
XPath is based on a Unix-style record system, in the form of path expressions. The basic elements of these location paths are nodes, relative coordinates, node tests, and predicates.
Node types:
| Relative coordinates: |
What is it used for?
If we have a shopping list from the previous month and want to base ourselves on it for this month, using XPath we can modify some elements such as quantities, include new quantities, or add or delete products from that list.
It is used in website views and templates or any other template that inherits from a main view, usually, those views that have already been created by Odoo or by a third party. Odoo provides us with various types of views for our models such as purchase, inventory, sales, form view, tree view, kanban view, and more.
How to enhance the use of XPath?
Two very useful tools for testing our expressions and validating that what we are implementing will work, are XPather and the browser itself.
xpather.com is a tool that allows us to test our XPath. Here we can copy the code and if we press the CTRL key and move the mouse, we can see how some parts of our XML are selected and the XPath is updated at the top, although it is generated by the machine it has some defects, we can use it as a guide.
The second tool is your browser. There will be occasions when we already have the website of our Odoo instance deployed and we want to add new information. By deploying the developer tools you will find all the code that makes up the website and assuming you want to add before some element, an image, or extra information, you go to the inspected part and by right-clicking on the copy part, you select copy XPath, and you will have the XPath of that node generated by the machine.
To check the XPath, press CTRL+f and it will display a search field that will allow you to search for the part of the XPath you are working on.
How we use it.
Being an open-source software, one of the features of Odoo is the ability to customize and extend its functionality through the creation of custom modules, a process in which XPath is essential.
In Vauxoo, we use XPath to select specific elements in a view or template. For example, when we want to change the format of a table in a purchase view, we use XPath to specifically select the table in question and apply the desired changes. We also use XPath to create validation rules in a form, allowing us to validate specific fields based on certain conditions.
In addition, in Vauxoo we use XPath to create relationships between different elements in a view. For example, if we want to show a list of related products with a specific product in a product detail view, we use XPath to establish that relationship.
Another way in which XPath is useful is in the creation of custom filters. With XPath, we can create filters to select specific records in a list view. For example, if we want to move only products that have a price greater than $500, we use XPath to create a filter that selects only products with that feature.
In summary, we use it to customize and extend the functionality of our client's (and our own) Odoo databases. Its ability to select specific elements in a view, create validation rules, establish relationships, and create custom filters makes it a powerful language for our module development.
Now you can test and verify that what you modified still takes you to the element you want. If it works as you need, you can place it where you want to integrate it into your code. A very interactive way to test Odoo code is from the console. Test the XPath with the following text: $x("xpathgenerated + Enter.
Remember to always work on a copy when you are going to modify any functionality of Odoo and keep in mind that with some update or migration, you’ll have to do it again so it will continue to operate.
You, like our team of experts, use this and other languages to ensure that your developments have excellent performance.