UNPKG

@nodecfdi/cfdi-cleaner

Version:

Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0

50 lines (49 loc) 1.62 kB
import xpath from 'xpath'; import { namespaceXsi } from '#src/utils/constants'; export default 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: 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.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.select(xpathQuery, this._document); } }