【问题标题】:need help on searching multiple elements using lxml XPATH在使用 lxml XPATH 搜索多个元素时需要帮助
【发布时间】:2021-05-07 19:40:44
【问题描述】:

我有以下 XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Bdpo>
  <ListaRegistrosBdpo>
    <RegistroBdpo>
      <Empresa>03271</Empresa>
      <DataRegistro>2018-03-27</DataRegistro>
      <DataOcorrencia>2017-11-08</DataOcorrencia>
      <DataReconhecimento>2017-11-17</DataReconhecimento>
      <NumeroEvento>00008</NumeroEvento>
      <TipoEvento>6</TipoEvento>
      <PerdaRaizDataRegistro>2018-02-08</PerdaRaizDataRegistro>
      <PerdaRaizNumeroEvento>00250</PerdaRaizNumeroEvento>
      <Categoria>7</Categoria>
      <OrigemJudicial>1</OrigemJudicial>
      <ContabilizadaPsl>2</ContabilizadaPsl>
      <FuncaoNegocio>1</FuncaoNegocio>
      <CausaPerda>2</CausaPerda>
      <StatusPerda>2</StatusPerda>
      <ValorBruto>0.00</ValorBruto>
      <DescricaoEvento>123</DescricaoEvento>
      <IdInternaEvento>Pasta 95</IdInternaEvento>
    </RegistroBdpo>
    <RegistroBdpo>
      <Empresa>03271</Empresa>
      <DataRegistro>2018-03-27</DataRegistro>
      <DataOcorrencia>2017-11-08</DataOcorrencia>
      <DataReconhecimento>2017-11-17</DataReconhecimento>
      <NumeroEvento>00007</NumeroEvento>
      <TipoEvento>6</TipoEvento>
      <PerdaRaizDataRegistro>2018-02-08</PerdaRaizDataRegistro>
      <PerdaRaizNumeroEvento>00248</PerdaRaizNumeroEvento>
      <Categoria>7</Categoria>
      <OrigemJudicial>1</OrigemJudicial>
      <ContabilizadaPsl>2</ContabilizadaPsl>
      <FuncaoNegocio>1</FuncaoNegocio>
      <CausaPerda>2</CausaPerda>
      <StatusPerda>2</StatusPerda>
      <ValorBruto>0.00</ValorBruto>
      <DescricaoEvento>123</DescricaoEvento>
      <IdInternaEvento>Pasta 41</IdInternaEvento>
    </RegistroBdpo>
    (...)
  </ListaRegistrosBdpo>
</Bdpo>

我想搜索符合以下条件的 RegistroBdpo 元素:

  1. TipoEvento 标签等于 5 OR 6
  2. PerdaRaizNumeroEvento 标签等于 00250 并返回他们的 ID 标签 (IdInternaEvento)。

我已经尝试了以下代码:

import lxml.etree as ET
  
r = ET.parse(xml_file_location)

string = './/*RegistroBdpo[TipoEvento="5" or TipoEvento="6"][PerdaRaizNumeroEvento="00250"]'
[ reg.find('IdInternaEvento').text for reg in r.iterfind(string) ]

但我得到了错误:SyntaxError: invalid predicate

关于我做错了什么有什么想法吗?谢谢。

【问题讨论】:

    标签: python xpath lxml


    【解决方案1】:

    就 XPath 而言,您只需要 //RegistroBdpo[TipoEvento=5 or TipoEvento=6][PerdaRaizNumeroEvento="00534"]/IdInternaEvento。或者更确切地说是//RegistroBdpo[TipoEvento=5 or TipoEvento=6][PerdaRaizNumeroEvento="00250"]/IdInternaEvento,具体取决于文本中给出的值与您的代码示例。

    在带有 lxml 的 Python 中,您可以使用例如ids = r.xpath('//RegistroBdpo[TipoEvento=5 or TipoEvento=6][PerdaRaizNumeroEvento="00250"]/IdInternaEvento')

    【讨论】:

    • 我收到错误“lxml.etree.XPathEvalError: Error in xpath expression”
    • 我在 Windows 上使用 Python 3.7 和 lxml 4.6.3 尝试了代码,代码工作正常,并从您的示例输入中返回带有 Pasta 95 的元素。不确定您的尝试在哪里失败,或许可以编辑问题以显示您拥有的示例。
    猜你喜欢
    • 2019-01-31
    • 2022-12-09
    • 1970-01-01
    • 2020-05-16
    • 2017-11-08
    • 1970-01-01
    • 2017-08-03
    • 2019-11-23
    • 2014-05-20
    相关资源
    最近更新 更多