Page 1 of 1

XmlDocument class

Posted: Fri Jun 17, 2022 3:01 pm
by tdeschampsBluewest
Since Legacy Scripting won't be supported anymore in 2024, i'm trying to switch (no pun intended) to the new switch 2022 Typescript mode for variables in mode "text with script expression".


Before, for using a Switch variables, we used job.getVariableAsString("[SOMETHING]", s);
But now that this is no longer available we have to go through much more complex bypasses.
I tried to use the XmlDocument class but without success and can't find where i'm wrong :

Here is an example with a simple dataset,

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<root>
	<tag1>Hello world!</tag1>
</root>

Here is my script :

Code: Select all

// Your imports here

async function getXpath(job: Job, dataset: string, xpath: string): Promise<string> {
	let result:string|number|boolean;
	try {
		const datasetPath = await job.getDataset(dataset, AccessLevel.ReadOnly);
		const XML = XmlDocument.open(datasetPath);
		const NSmap = XML.getDefaultNSMap();
		const evaluation = XML.evaluate(xpath, NSmap);
		result = evaluation;
	} catch (e) {
		result = e.message;
	}
	return JSON.stringify(result);
}

// 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> {
	let result= ""
	const dataset = "Log";
	const xpath = "/root/tag1";
	result = await getXpath(job, dataset, xpath)
	return result
}

The typeof result in the main function is "undefined"

Every bit of Help will be appreciated! :oops:

Re: XmlDocument class

Posted: Mon Jun 20, 2022 11:49 am
by tdeschampsBluewest
Additional note:
The script does not return an error, and does not go into the "catch" of the getXpath function.
I tried with different ways to write the Xpath (without the first /, with double "//")

Re: XmlDocument class

Posted: Mon Jul 04, 2022 1:38 pm
by laurentd
Hi Thomas,
Here is the trick:
const xpath = "string(/root/tag1)";

Re: XmlDocument class

Posted: Tue Jul 05, 2022 11:39 pm
by tdeschampsBluewest
Thanks a lot Laurent,

Here is my function now, and it work perfectly :

Code: Select all

async function getXpath(job: Job, dataset: string, xpath: string): Promise<string> {
	let result:string|number|boolean;
	try {
		const datasetPath = await job.getDataset(dataset, AccessLevel.ReadOnly);
		const XML = XmlDocument.open(datasetPath);
		const NSmap = XML.getDefaultNSMap();
		const evaluation = XML.evaluate(`string(${xpath})`, NSmap);
		result = evaluation;
	} catch (e) {
		result = e.message;
	}
	return JSON.stringify(result);
}