Odoo V14定制stock_picking报表触发XPathSyntaxError错误求助
问题排查与修复方案
错误原因
你遇到的lxml.etree.XPathSyntaxError: Invalid predicate错误由第二个XPath表达式的语法问题直接导致:书写匹配table节点的谓语条件时,你漏掉了谓语结束的右中括号],XPath解析器无法识别不完整的条件表达式,直接抛出语法错误。
修复后的代码
<?xml version="1.0" encoding="utf-8"?> <odoo> <template id="report_picking_inherit" inherit_id="stock.report_picking"> <xpath expr="//td[contains(@t-if, 'has_barcode') and contains(@class, 'text-center')]" position="after"> <td> <span t-if="o.state != 'done'" t-esc="ml.product_id.weight"/> <span t-if="o.state == 'done'" t-esc="ml.product_id.weight"/> </td> </xpath> <!-- 补全了谓语结束的] 修复语法错误 --> <xpath expr="//table[contains(@t-if, 'o.move_line_ids') and contains(@t-if, 'and') and contains(@t-if, 'o.move_ids_without_package')]" position="after"> <span t-field="o.stock_total_weight"/> </xpath> </template> </odoo>
优化建议
- 当前通过匹配
t-if属性内容定位节点的方式非常脆弱,只要官方模板调整t-if条件的书写顺序、增减空格,XPath就会匹配失败。建议优先使用更稳定的定位条件:比如匹配节点的class属性、父节点的唯一标识,或者节点下的固定子节点。 - 第一个XPath中的两个span逻辑完全一致,可以合并成一行,不需要重复写
t-if判断:<span t-esc="ml.product_id.weight"/>即可。
内容的提问来源于stack exchange,提问作者Gbn
相关产品推荐
相关产品推荐

