Usign xpath trough script

Post Reply
User avatar
Soul Forge
Member
Posts: 61
Joined: Wed Jul 12, 2023 5:25 pm

Usign xpath trough script

Post by Soul Forge »

Hello!

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;
}
I've already tried using import instead of require, like I'm doing with fs, but that didn't work either.

When jobs run the script, this is the error I get.
Cannot find module 'xpath' Require stack: - C:\Program Files\Enfocus\Enfocus Switch\ScriptExecutor\NodeScriptExecutor.js
Any help would be appreciated, thanks in advance!
Padawan
Advanced member
Posts: 364
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Usign xpath trough script

Post by Padawan »

You cannot use modules in script expressions which are not included in Nodejs itself. The correct way to read xml files is to use the native Switch modules, you can find more information in the documentation:

https://www.enfocus.com/manuals/Develop ... thods.html
Post Reply