In practice, CFDI files received from third parties are often technically valid under SAT rules but contain XML errors that break strict parsing or validation. This happens because the SAT (or a PAC acting on the SAT’s behalf) signs the CFDI and then allows additional content — such asDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/eclipxe13/CfdiUtils/llms.txt
Use this file to discover all available pages before exploring further.
cfdi:Addenda data — to be appended after signing. That post-signing content is outside the cadena de origen and does not invalidate the digital seal, but it frequently introduces XML issues that cause downstream processing failures.
The CfdiUtils\Cleaner\Cleaner class repairs these common problems before you pass the XML to the Cfdi constructor or a validator.
What the Cleaner Does
Callingclean() on a loaded Cleaner object applies the following operations in order:
- Fix incorrect
xmlns:schemaLocation— Some SAT-issued CFDI documents incorrectly usexmlns:schemaLocationinstead ofxsi:schemaLocation. This is corrected as a string operation before the XML is parsed. - Remove bare CFDI 3 namespace declaration — If the document declares
xmlns="http://www.sat.gob.mx/cfd/3"(no prefix) alongside the correct prefixed declarationxmlns:cfdi="http://www.sat.gob.mx/cfd/3", the bare declaration is removed. - Remove
cfdi:Addenda— The entirecfdi:Comprobante/cfdi:Addendanode is removed. Its content is outside the cadena de origen and is a common source of invalid XML. - Remove incomplete
schemaLocationentries — Pairs inxsi:schemaLocationwhere the second URI does not end in.xsdare removed. - Remove non-SAT namespace nodes — All XML elements whose namespace does not start with
http://www.sat.gob.mx/orhttp://www.w3.org/are removed. - Remove non-SAT
schemaLocationpairs — Namespace/XSD pairs inxsi:schemaLocationthat reference non-SAT namespaces are removed. - Remove unused namespace declarations — Any
xmlns:*declarations that are no longer referenced after the previous steps are stripped. - Collapse multiple
cfdi:Complementonodes — The Anexo 20 specification allows only onecfdi:Complemento, but the XSD permits many. Multiple occurrences are merged into a single node, preserving child order so the cadena de origen remains identical. - Fix known SAT XSD URLs — Known incorrect or outdated XSD location URLs for CFDI and Timbre Fiscal Digital are corrected.
Basic Usage
Static one-liner
The fastest way to clean a CFDI string is the static helperCleaner::staticClean(). It creates a Cleaner internally, calls clean(), and returns the cleaned XML string.
Object-oriented usage
Create aCleaner instance, then call clean() followed by retrieveXml():
Individual Cleaning Operations
If you want finer control — for example, to skip certain steps or run them in a different order — you can call each operation individually on a loadedCleaner instance:
| Method | What it does |
|---|---|
removeAddenda() | Removes the cfdi:Comprobante/cfdi:Addenda node |
removeIncompleteSchemaLocations() | Drops xsi:schemaLocation pairs where the XSD URI does not end in .xsd |
removeNonSatNSNodes() | Removes all elements whose namespace is not SAT-owned |
removeNonSatNSschemaLocations() | Removes non-SAT pairs from all xsi:schemaLocation attributes |
removeUnusedNamespaces() | Removes xmlns:* declarations that are no longer in use |
collapseComprobanteComplemento() | Merges multiple cfdi:Complemento elements into one |
fixKnownSchemaLocationsXsdUrls() | Corrects known incorrect SAT XSD URLs |
Skipping the Pre-load Cleaning Steps
The first two operations (fixingxmlns:schemaLocation and removing the bare CFDI 3 namespace) are applied as string transformations before the XML is parsed. They are handled by an injected BeforeLoadCleanerInterface implementation. To skip them, pass a null-object implementation to the constructor:
Version Support
TheCleaner only processes CFDI versions 3.2, 3.3, and 4.0. You can check whether a given version string is supported with the static helper:
Namespace Allowlist
The cleaner keeps namespaces that belong to the SAT or to W3C standards. You can check whether any namespace URI would be preserved with:When to Use
Receiving CFDI from third parties
Always clean CFDI files received from clients, suppliers, or data feeds before parsing or validating them. Post-signing additions are common and frequently contain XML errors.
Before validation
Run the cleaner before feeding the XML into the CfdiUtils validators. Validation errors caused by extraneous namespaces or a malformed
Addenda can mask genuine compliance issues.