@nodecfdi/cfdi-cleaner
Version:
Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0
29 lines (28 loc) • 1.04 kB
JavaScript
export default class XmlNsSchemaLocation {
clean(xml) {
if (!xml.includes('xmlns:schemaLocation="')) {
// Nothing to do
return xml;
}
const pattern = /<[\s\S]*?>/gu;
const matches = [...xml.matchAll(pattern)];
const parts = xml.split(pattern);
const buffer = [parts[0]];
for (const [index, match] of matches.entries()) {
buffer.push(this.cleanTagContent(match[0]), parts[index + 1]);
}
return buffer.join('');
}
cleanTagContent(content) {
if (!content.includes('xmlns:schemaLocation="')) {
// Nothing to do
return content;
}
if (!content.includes('xsi:schemaLocation="')) {
// Safely replace to "xsi:schemaLocation"
return content.replaceAll(/(\s)xmlns:schemaLocation="/g, '$1xsi:schemaLocation="');
}
// Remove xmlns:schemaLocation attribute
return content.replaceAll(/\sxmlns:schemaLocation="[\s\S]*?"/g, '');
}
}