Search code examples
phpxml

Delete schemaLocation of an XML


I have an XML and I want to be able to edit a value with PHP, all the XML that I receive and upload to my database bring in xsi:schemaLocation inside the Comprobante this node

http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd

However, some that have addenda bring more links which I want to eliminate and leave only what I put above.

I give you an example an XML.

<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" 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.walmartmexico.com.mx http://www.walmartmexico.com.mx/AddendaKioscos.xsd" Version="4.0"  Fecha="2023-04-20T16:23:57" Moneda="MXN" TipoCambio="1" SubTotal="2074.12" Descuento="46.62" Total="2351.90" FormaPago="04" CondicionesDePago="Inmediato" TipoDeComprobante="I" MetodoPago="PUE" LugarExpedicion="03310" NoCertificado="" Exportacion="01" Serie="ICABM" Folio="" Sello="" xmlns:otro="http://www.walmartmexico.com.mx">
  <cfdi:Emisor Rfc="NWM9709244W4" Nombre="NUEVA WAL MART DE MEXICO" RegimenFiscal="601" />
  <cfdi:Receptor Rfc="XXXXXX" Nombre="XXXX" UsoCFDI="G03" DomicilioFiscalReceptor="03100" RegimenFiscalReceptor="601" />
  <cfdi:Conceptos>
</cfdi:Comprobante>

The value that I want to edit inside the Comprobante is this:

xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.walmartmexico.com.mx http://www.walmartmexico.com.mx/AddendaKioscos.xsd"

Solution

  • I was able to solve it using cfdiUtils, I leave my code in case anyone is interested:

        $docxml =file_get_contents('XML PATH');
    
        $cleaner = new Cleaner($docxml);
        $cleaner->removeAddenda();
        $cleaner->removeNonSatNSschemaLocations();
        $content = $cleaner->retrieveXml();