简体   繁体   English

如何从具有不同名称空间的子项中获取lxml.objectify项目?

[英]How to get lxml.objectify items from children with different namespace?

I have the following python script: 我有以下python脚本:

from lxml import objectify
xml = objectify.fromstring("""<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd">
  <cfdi:Emisor rfc="XYZU8801017YA" nombre="MOYLOP260">
    <cfdi:DomicilioFiscal calle="Calle value"/>
    <cfdi:RegimenFiscal Regimen="Regimen value" />
  </cfdi:Emisor>
  <cfdi:Complemento>
    <tfd:TimbreFiscalDigital xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/TimbreFiscalDigital/TimbreFiscalDigital.xsd" xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital"
        version="1.0" UUID="UUID value"/>
  </cfdi:Complemento>
</cfdi:Comprobante>""")
print "xml.Emisor.DomicilioFiscal.get('calle'):", xml.Emisor.DomicilioFiscal.get('calle')
print "xml.Emisor.RegimenFiscal.get('Regimen'):", xml.Emisor.RegimenFiscal.get('Regimen')
tfd = xml.Complemento.xpath('tfd:TimbreFiscalDigital[1]',
                            namespaces={'tfd': 'http://www.sat.gob.mx/TimbreFiscalDigital'})
print "tfd[0].get('UUID'):", tfd[0].get('UUID')
try:
    print "xml.Complemento.TimbreFiscalDigital: ", xml.Complemento.TimbreFiscalDigital.get('UUID')
except AttributeError:
    print "Why I have a AttributeError here?"

The output is: 输出为:

xml.Emisor.DomicilioFiscal.get('calle'): Calle value
xml.Emisor.RegimenFiscal.get('Regimen'): Regimen value
tfd[0].get('UUID'): UUID value
xml.Complemento.TimbreFiscalDigital:  Why I have a AttributeError here?

I need get the value UUID from the last node but I don't like use a hard-coded xml namespace from xpath because this one is defined from xml string. 我需要从最后一个节点获取值UUID,但我不喜欢从xpath使用硬编码的xml名称空间,因为这是从xml字符串定义的。

Could you help me? 你可以帮帮我吗? Thanks! 谢谢!

Do I need update the namespaces from children? 我需要从子级更新名称空间吗?

According to http://lxml.de/objectify.html#namespace-handling , you need to provide the namespace of the child when doing lookups: 根据http://lxml.de/objectify.html#namespace-handling ,在执行查找时,您需要提供子级的名称空间:

tfd = xml.Complemento["{http://www.sat.gob.mx/TimbreFiscalDigital}TimbreFiscalDigital"]

Alternatively: 或者:

tfd = getattr(xml.Complemento, "{http://www.sat.gob.mx/TimbreFiscalDigital}TimbreFiscalDigital")

The only way (that I can think of) to get a specific child element without specifying the child's namespace is to use local-name() : (我能想到的)获取特定子元素而不指定子元素名称空间的唯一方法是使用local-name()

tfd = xml.Complemento.xpath("*[local-name() = 'TimbreFiscalDigital']")[0]
print tfd.get("UUID")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM