XmlDocument class

Post Reply
tdeschampsBluewest
Member
Posts: 34
Joined: Tue Jun 01, 2021 11:57 am

XmlDocument class

Post 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:
tdeschampsBluewest
Member
Posts: 34
Joined: Tue Jun 01, 2021 11:57 am

Re: XmlDocument class

Post 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 "//")
laurentd
Member
Posts: 137
Joined: Wed Mar 13, 2019 2:06 pm

Re: XmlDocument class

Post by laurentd »

Hi Thomas,
Here is the trick:
const xpath = "string(/root/tag1)";
Laurent De Wilde, Solution Architect @ Enfocus
tdeschampsBluewest
Member
Posts: 34
Joined: Tue Jun 01, 2021 11:57 am

Re: XmlDocument class

Post 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);
}
Post Reply