How could I go about using xpath in script?
I'm building a flow that's going to identify the errors from jobs that come from other flows, and I want it to be able to identify the error log file from multiple applications.
But I'm not able to use xpath. I'm trying to import the xmldom and xpath libraries from node js, but they are not working inside the script expression, not even their intelisense show up.
Here's what I've got until now:
Code: Select all
// Your imports here
import * as fs from 'fs';
const xpath = require('xpath');
const DOMParser = require('@xmldom/xmldom');
// Do not remove calculateScriptExpression function. It must return a promise to the property value.
async function calculateScriptExpression(s: Switch, flowElement: FlowElement, job: Job): Promise<string> {
const jobErroOrigem:string = await job.getPrivateData('jobErroOrigem');
let jobErrosNomes:string;
await job.log(LogLevel.Debug, `Processing errors from ${jobErroOrigem}`);
switch(jobErroOrigem) {
case 'Pitstop':
jobErrosNomes = await errosPitstop(job);
break;
}
return jobErrosNomes;
}
async function errosPitstop(job): Promise<string> {
const jobErrosXmlPath:string = await job.getDataset('Erros', AccessLevel.ReadOnly);
const jobErrosXml:string = fs.readFileSync(jobErrosXmlPath, {encoding: 'utf-8'});
const jobErrosID:string = await evaluateXpath(jobErrosXml, "//PreflightReport/Errors/PreflightReportItem/@ActionID", job);
const jobsErrosNomes:string[] = [];
await job.log(LogLevel.Debug, `Erros XML Path: ${jobErrosXmlPath} Erros XML: ${jobErrosXml}`);
for (let i = 0; i < jobErrosID.length; i++) {
const erroID = jobErrosID[i];
switch(erroID) {
case "1150":
jobsErrosNomes.push("Imagem em baixa");
break;
case "1164":
jobsErrosNomes.push("Paginas Tamanhos Diferentes");
break;
default:
jobsErrosNomes.push(erroID);
break;
}
}
await job.log(LogLevel.Debug, jobsErrosNomes.join(" e "));
return jobsErrosNomes.join(" e ")
}
async function evaluateXpath(xml:string, xpathString:string, job): Promise<string> {
const doc = new DOMParser.parseFromString(xml, 'text/xml');
const xpathResult = xpath.select(xpathString, doc)[0];
await job.log(LogLevel.Debug, `xpath result: ${xpathResult}`);
return xpathResult;
}
When jobs run the script, this is the error I get.
Any help would be appreciated, thanks in advance!Cannot find module 'xpath' Require stack: - C:\Program Files\Enfocus\Enfocus Switch\ScriptExecutor\NodeScriptExecutor.js