0

How can I create this XML structure with Python and lxml?

<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:cce11="http://www.sat.gob.mx/ComercioExterior11" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/ComercioExterior11 http://www.sat.gob.mx/sitio_internet/cfd/ComercioExterior11/ComercioExterior11.xsd" Version="4.0" Fecha="2023-12-27T11:53:50">
    <cfdi:Emisor Rfc="XAXX010101XXX" Nombre="COMPANY" RegimenFiscal="601"/>
    <cfdi:Receptor Rfc="XEXX010101XXX" Nombre="COMPANY" DomicilioFiscalReceptor="00000" RegimenFiscalReceptor="601" UsoCFDI="G01"/>
    <cfdi:Conceptos>
        <cfdi:Concepto ClaveProdServ="00000000" NoIdentificacion="XXXXX" Cantidad="1.000000" ClaveUnidad="EA" Unidad="PIEZA" Descripcion="XXXXX" ValorUnitario="1.00" Importe="1.00" ObjetoImp="00">
            <cfdi:Impuestos>
                <cfdi:Traslados>
                    <cfdi:Traslado Base="1.00" Importe="1.00" Impuesto="000" TipoFactor="Tasa" TasaOCuota="0.000000"/>
                </cfdi:Traslados>
            </cfdi:Impuestos>
        </cfdi:Concepto>
    </cfdi:Conceptos>
    <cfdi:Impuestos TotalImpuestosTrasladados="1.00">
        <cfdi:Traslados>
            <cfdi:Traslado Base="1.00" Importe="1.00" Impuesto="000" TipoFactor="Tasa" TasaOCuota="0.000000"/>
            <cfdi:Traslado Base="1.00" Importe="1.00" Impuesto="000" TipoFactor="Tasa" TasaOCuota="0.000000"/>
        </cfdi:Traslados>
    </cfdi:Impuestos>
</cfdi:Comprobante>

This structure is used for Mexican invoices.

4

1 Answer 1

-2

Using a Powershell script. I hardcoded the values. The total I also hardcoded.

using assembly System.Xml.Linq 

$filename = 'c:\temp\test.xml'
$emisorRfc = 'XAXX010101XXX'
$receptorRfc = 'XEXX010101XXX'
$nombre = 'COMPANY' 
$regimenFiscal = '601'

$domicilioFiscalReceptor = '00000'
$regimenFiscalReceptor = '601' 
$usoCFDI = 'G01'

$claveProdServ = '00000000'
$noIdentificacion = 'XXXXX' 
$cantidad = '1.000000'
$claveUnidad = 'EA' 
$unidad = 'PIEZA' 
$descripcion = 'XXXXX' 
$valorUnitario = '1.00' 
$importe = '1.00' 
$objetoImp = '00'

$base = '1.00' 
$importe = '1.00' 
$impuesto = '000' 
$tipoFactor = 'Tasa' 
$tasaOCuota = '0.000000'

$ident = @'
<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:cce11="http://www.sat.gob.mx/ComercioExterior11" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/ComercioExterior11 http://www.sat.gob.mx/sitio_internet/cfd/ComercioExterior11/ComercioExterior11.xsd" Version="4.0">
</cfdi:Comprobante>
'@

$xDoc = [System.Xml.Linq.XDocument]::Parse($ident)
$root = $xDoc.Root

$date = [DateTime]::Now
$dateStr = $date.ToString('yyyy-MM-ddTHH:mm:ss')
$fecha = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Fecha'), $dateStr)
$root.Add($fecha)

$nscfdi = $root.GetNamespaceOfPrefix('cfdi')
$nscce11 = $root.GetNamespaceOfPrefix('cce11')
$nsxsi = $root.GetNamespaceOfPrefix('xsdi')

$emisor = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Emisor'))

$emisorRfcAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('RFC'), $emisorRfc)
$emisor.Add($emisorRfcAttr)

$emisorNombre = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Nombre'), $nombre)
$emisor.Add($emisorNombre)

$emisorRegimen = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('RegimenFiscal'), $regimenFiscal)
$emisor.Add($emisorRegimen)

$root.Add($emisor)

$receptor = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Receptor'))

$receptorRfcAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('RFC'), $receptorRfc)
$receptor.Add($receptorRfcAttr)

$receptor.Add($emisorNombre)

$receptorDomicilioFiscal = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('DomicilioFiscalReceptor'), $domicilioFiscalReceptor)
$receptor.Add($receptorDomicilioFiscal)

$receptorRegimenFiscal = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('RegimenFiscalReceptor'), $regimenFiscalReceptor)
$receptor.Add($receptorRegimenFiscal)

$receptorUsoCFDI = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('UsoCFDI'), $usoCFDI)
$receptor.Add($receptorUsoCFDI)

$root.Add($receptor)

$conceptos = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Conceptos'))

$concepto = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Concepto'))

$claveProdServAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('ClaveProdServ'), $claveProdServ)
$concepto.Add($claveProdServAttr)

$noIdentificacionAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('NoIdentificacion'), $noIdentificacion)
$concepto.Add($noIdentificacionAttr)

$cantidadAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Cantidad'), $cantidad)
$concepto.Add($cantidadAttr)

$claveUnidadAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('ClaveUnidad'), $claveUnidad)
$concepto.Add($claveUnidadAttr)

$unidadAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Unidad'), $unidad)
$concepto.Add($unidadAttr)

$descripcionAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Descripcion'), $descripcion)
$concepto.Add($descripcionAttr)

$valorUnitarioAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('ValorUnitario'), $valorUnitario)
$concepto.Add($valorUnitarioAttr)

$importeAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Importe'), $importe)
$concepto.Add($importeAttr)

$objetoImpAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('ObjetoImp'), $objetoImp)
$concepto.Add($objetoImpAttr)

$impuestos = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Impuestos'))

$traslados = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Traslados'))

$traslado = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Traslado'))

$baseAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Base'), $base)
$traslado.Add($baseAttr)

$importeAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Importe'), $importe)
$traslado.Add($importeAttr)

$impuestoAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('Impuesto'), $impuesto)
$traslado.Add($impuestoAttr)

$tipoFactorAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('TipoFactor'), $tipoFactor)
$traslado.Add($tipoFactorAttr)

$tasaOCuotaAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('TasaOCuota'), $tasaOCuota)
$traslado.Add($tasaOCuotaAttr)

$traslados.Add($traslado)

$impuestos.Add($traslados)

$concepto.Add($impuestos)

$conceptos.Add($concepto)

$root.Add($conceptos)

$impuestos = [System.Xml.Linq.XElement]::new([System.Xml.Linq.XName]::Get($nscfdi + 'Impuestos'))

$totalImpuestosTrasladados = '1.00'

$totalImpuestosTrasladadosAttr = [System.Xml.Linq.XAttribute]::new([System.Xml.Linq.XName]::Get('TotalImpuestosTrasladados'), $totalImpuestosTrasladados)
$impuestos.Add($totalImpuestosTrasladadosAttr)

$root.Add($impuestos)

$xDoc.Save($filename)
Sign up to request clarification or add additional context in comments.

4 Comments

The question was ...with Python and lxml ...!
@Hermann12 : Alternate solutions are allowed at this website.
@jdweng the question is about python, if you can read. Therefore your answer isn’t suitable, I think. It could be that the OP works on Linux, or iOS.
@Hermann12 : There is nothing wrong with post alternate answer especially since nobody posted a python solution. Do not guess. PS can run on Linux and Mac. PS is written in Net Library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.