Page 1 of 1

Filter XML node by type then use IF THEN

Posted: Thu May 05, 2016 1:06 pm
by abonsey
I have an XML with multiple nodes that are "Extrinsic" and need to filter it to one node of Type 'X'. Once that has been filtered correctly I need to use IF THEN see what the variable is and then use a NEW variable further in the process.

Can this be done via scripting?

Re: Filter XML node by type then use IF THEN

Posted: Fri May 06, 2016 1:29 pm
by BuckXUK
Hi Andrew

Yes, you can do all this with scripting. You'll just need the right XPath syntax to identify your target Extrinsic node.

Re: Filter XML node by type then use IF THEN

Posted: Fri May 06, 2016 1:41 pm
by abonsey
Hi William,
Just PM'd you with a few questions.

Andrew

Re: Filter XML node by type then use IF THEN

Posted: Mon May 09, 2016 5:45 pm
by abonsey
Here is a sample of the XML.
How could I get the "Stock" information?

Thanks for any help

Andrew

<Request type="Order" ID="2929" Date="2016-05-03T15:28:29">
<Order Version="1.0" CartNumber=“1234” Date="2016-05-03T15:28:29">
</Order>
<Items>
<Item LineNumber="6039">
<Extrinsic Type="NameOnStationery"></Extrinsic>
<Extrinsic Type="Flat Size" ExternalId="" ExternalTypeId="">A4</Extrinsic>
<Extrinsic Type="Folding" ExternalId="" ExternalTypeId="">Roll</Extrinsic>
<Extrinsic Type="Lamination" ExternalId="" ExternalTypeId="">Not Required</Extrinsic>
<Extrinsic Type="Quantity" ExternalId="" ExternalTypeId="">200</Extrinsic>
<Extrinsic Type="Sides Printed" ExternalId="" ExternalTypeId="">Double Sided</Extrinsic>
<Extrinsic Type="Stock" ExternalId="" ExternalTypeId="">135gsm Silk</Extrinsic>
<Extrinsic Type="Turnaround" ExternalId="" ExternalTypeId="">Express (5 Bus Days)</Extrinsic>
</Item>
</Items>
</Request>

Re: Filter XML node by type then use IF THEN

Posted: Mon May 09, 2016 6:06 pm
by gabrielp
You should be able to use the XML class within Scripter to grab that value with an XPath somewhat like this:

Code: Select all

/Request/Items/Item/Extrinsic[@Type='Stock']

Re: Filter XML node by type then use IF THEN

Posted: Mon May 09, 2016 6:29 pm
by abonsey
Hi,
Could you clarify "XML class within scripter". I've never done anything like this before.

Thanks
Andrew

Re: Filter XML node by type then use IF THEN

Posted: Mon May 09, 2016 6:36 pm
by gabrielp
abonsey wrote:Hi,
Could you clarify "XML class within scripter". I've never done anything like this before.

Thanks
Andrew
Do you have the scripting + XML modules? If not, we'll have to come up with a different solution.

This page in the manual gives a good overview: http://www.enfocus.com/manuals/Develope ... sions.html

Re: Filter XML node by type then use IF THEN

Posted: Mon May 09, 2016 6:44 pm
by abonsey
Hi,
Yes I do have the scripting module.

I start with:
function jobArrived( s : Switch, job : Job )

{
var dataset = job.getDataset("Xml");
var xml = new Document(dataset);
var Stock = xml.evalToString(/Request/Items/Item/Extrinsic[@Type='Stock']);

but it errors.... clearly it's wrong :(

Re: Filter XML node by type then use IF THEN

Posted: Mon May 09, 2016 7:05 pm
by gabrielp
I updated the link in my last post, that copied over wrong.
but it errors.... clearly it's wrong :(
Don't let it stop there. In this field, you'll hit speed bumps like this a lot. Own the problem! What's the error say? Start there.

For now, this line appears to be incorrect. evalToString() expects a string. That string can be passed as a parameter in quotes or a variable.

Code: Select all

var Stock = xml.evalToString(/Request/Items/Item/Extrinsic[@Type='Stock']);
Probably should read:

Code: Select all

var Stock = xml.evalToString("/Request/Items/Item/Extrinsic[@Type='Stock']");

Re: Filter XML node by type then use IF THEN

Posted: Tue May 10, 2016 12:42 pm
by abonsey
Thanks for the help. All working great now.
Onwards and Upwards from here :D

Andrew

Re: Filter XML node by type then use IF THEN

Posted: Thu May 12, 2016 3:38 pm
by abonsey
HI,
I've been looking to improve/simplify the script by using a using search/replace via dictionary/key so that the same words are replaced throughout the whole xml.

I've probably got it all wrong but here's what I've got so-far that doesn't work.

Any ideas?

function jobArrived( s : Switch, job : Job )

{
// Read incoming XML
var oldXML = File.read(job.getPath(), "Windows-1252");
var oldXML = oldXML.evalToString()

// Create new XML
var newXML = job.createPathWithName(job.getName());
var newXMLcontent = oldXML.mapReplace({ 115gsm Silk: 'CD S 115', 135gsm Silk: 'CD S 135', 150gsm Silk: 'CD S 150', 170gsm Silk: 'CD S 170'})


// Replace stuff
// These properties can easily be filled via standard Switch properties, see screenshot.
var newXMLcontent = oldXML

// Write new XML
File.write(newXML, newXMLcontent, 'Windows-1252');

// Send newXML to next folder
job.sendToSingle(newXML);
}

Re: Filter XML node by type then use IF THEN

Posted: Thu May 12, 2016 4:20 pm
by gabrielp
abonsey wrote:here's what I've got so-far that doesn't work.
What errors are you getting?

This line looks problematic. You have spaces in your object keys, which is not valid.

Code: Select all

var newXMLcontent = oldXML.mapReplace({ 115gsm Silk: 'CD S 115', 135gsm Silk: 'CD S 135', 150gsm Silk: 'CD S 150', 170gsm Silk: 'CD S 170'})
To make the object valid, you'd have to quote those keys:

Code: Select all

var newXMLcontent = oldXML.mapReplace({ '115gsm Silk': 'CD S 115', '135gsm Silk': 'CD S 135', '150gsm Silk': 'CD S 150', '170gsm Silk': 'CD S 170'})
But I'm not familiar with the mapReplace method or how that is supposed to work.