@nodecfdi/cfdi-cleaner
Version:
Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0
56 lines (55 loc) • 1.91 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const xpath_1 = __importDefault(require("xpath"));
const constants_1 = require("#src/utils/constants");
class CfdiXPath {
static ALLOWED_NAMESPACES = [
'http://www.sat.gob.mx/cfd/3',
'http://www.sat.gob.mx/cfd/4',
];
_document;
_namespaces;
constructor(document, namespaces) {
this._document = document;
this._namespaces = namespaces;
}
static createFromDocument(document) {
const root = document.documentElement;
let rootNamespace = root?.namespaceURI ?? '';
if (!this.ALLOWED_NAMESPACES.includes(rootNamespace)) {
rootNamespace = '';
}
const namespaces = {
cfdi: rootNamespace,
xsi: constants_1.namespaceXsi,
};
return new CfdiXPath(document, namespaces);
}
queryElements(xpathQuery) {
return this.querySelect(xpathQuery);
}
queryAttributes(xpathQuery) {
return this.querySelect(xpathQuery);
}
/**
* Get all XMLSchema instance attributes schemaLocation
*
* @returns Attr[]
*/
querySchemaLocations() {
return this.queryAttributes('//@xsi:schemaLocation');
}
querySelect(xpathQuery) {
if (this._namespaces && Object.entries(this._namespaces).length > 0) {
const selectWithNS = xpath_1.default.useNamespaces(this._namespaces);
// @ts-expect-error misssing Node properties are not needed
return selectWithNS(xpathQuery, this._document);
}
// @ts-expect-error misssing Node properties are not needed
return xpath_1.default.select(xpathQuery, this._document);
}
}
exports.default = CfdiXPath;