-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
utils.py
46 lines (37 loc) · 1.56 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright 2020 ACSONE SA/NV
# Copyright 2022 Camptocamp SA
# @author Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from lxml import etree
from odoo.tools import pycompat
def xml_purge_nswrapper(xml_content):
"""Purge `nswrapper` elements.
QWeb template does not allow parsing namespaced elements
without declaring namespaces on the root element.
Hence, by default you cannot define smaller re-usable templates
if the have namespaced elements.
The trick is to wrap your reusable template with `nswrapper` element
which holds the namespace for that particular sub template.
For instance:
<nswrapper xmlns:foo="http://www.unece.org/cefact/Foo">
<foo:LovelyNamespacedElement />
</nswrapper>
Then this method is going to purge these unwanted elements from the result.
"""
if not (xml_content and xml_content.strip()):
return xml_content
root = etree.XML(xml_content)
# Deeper elements come after, keep the root element at the end (if any).
# Use `name()` because the real element could be namespaced on render.
for nswrapper in reversed(root.xpath("//*[name() = 'nswrapper']")):
parent = nswrapper.getparent()
if parent is None:
# fmt:off
return "".join([
pycompat.to_text(etree.tostring(el))
for el in nswrapper.getchildren()
])
# fmt:on
parent.extend(nswrapper.getchildren())
parent.remove(nswrapper)
return etree.tostring(root)