[Python] How to get attributes values from an xml file (CFDI - SAT Mexico)

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a file where i can not read the attributes since the tag, looks different than usual, i have tried to use lxml as follow:

    from lxml import etree

    xDoc = etree.parse(xmlFile)
    folioFiscal = xDoc.find('tfd:TimbreFiscalDigital')
    print(folioFiscal)


    the xml file is as follow:

    <?xml version="1.0" encoding="utf-8"?>
    <cfdi:Comprobante xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" Serie="AA" Folio="147" Fecha="2025-01-01T17:51:12" Sello="[alfanumeric_values]" FormaPago="03" NoCertificado="[numeric_values]" Certificado="[alfanumeric_values]" SubTotal="###.00" Moneda="MXN" Total="###.00" TipoDeComprobante="I" Exportacion="01" MetodoPago="PUE" LugarExpedicion="#####" xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cfdi:Emisor Rfc="[A-Z]{4}[0-9]{6}[A-Z]{3}" Nombre="[alfanumeric_values]" RegimenFiscal="###" />
    <cfdi:Receptor Rfc="[A-Z]{4}[0-9]{6}[A-Z]{3}" Nombre="[alfanumeric_values]" DomicilioFiscalReceptor="#####" RegimenFiscalReceptor="###" UsoCFDI="D01" />
    <cfdi:Conceptos>
    <cfdi:Concepto ClaveProdServ="########" Cantidad="1.00" ClaveUnidad="ACT" Unidad="Actividad" Descripcion="Honorarios Medicos" ValorUnitario="1100.00" Importe="1100.00" ObjetoImp="02">
    <cfdi:Impuestos>
    <cfdi:Traslados>
    <cfdi:Traslado Base="1100.00" Impuesto="002" TipoFactor="Exento" />
    </cfdi:Traslados>
    </cfdi:Impuestos>
    </cfdi:Concepto>
    </cfdi:Conceptos>
    <cfdi:Impuestos>
    <cfdi:Traslados>
    <cfdi:Traslado Base="1100.00" Impuesto="002" TipoFactor="Exento" />
    </cfdi:Traslados>
    </cfdi:Impuestos>
    <cfdi:Complemento>
    <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd" Version="1.1" UUID="[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}" FechaTimbrado="2025-01-01T17:51:34" RfcProvCertif="SAT[A-Z0-9]{8}" SelloCFD="[alfanumeric_values]" NoCertificadoSAT="[0-9]{20}" SelloSAT="[alfanumeric_values]" />
    </cfdi:Complemento>
    </cfdi:Comprobante>


    I want to obtain the values in UUID attribute that looks like [A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}.

    Continue reading...

Compartilhe esta Página