skip to Main Content

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"

2

Answers


  1. Chosen as BEST ANSWER

    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();
    

  2. You can edit the XML using DOM. In DOM anything is a node. This includes attributes. xsi:schemaLocation is an attribute node inside a namespace, the XML parser will resolve it to {http://www.w3.org/2001/XMLSchema-instance}schemaLocation using the value from the xmlns:xsi namespace definition.

    // bootstrap the XML
    $document = new DOMDocument();
    $document->loadXML(getXMLString());
    // access the attribute node ...
    $attribute = $document
      // ... on the document element (the "root" element) ...
      ->documentElement
      // ... using the namespace aware method (suffix "*NS")
      ->getAttributeNodeNS(
        'http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation'
      );
      
    // read attribute value
    $schemaLocation = $attribute->value;
    
    // edit value
    $urls = array_diff(
        explode(' ', $schemaLocation),
        [
          'http://www.sat.gob.mx/cfd/4'
        ]
    );
    
    // write new attribute value
    $attribute->textContent = implode(' ', $urls);
      
    echo $document->saveXML();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search