You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 10:45:01