Filter XML node by type then use IF THEN

Post Reply
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Filter XML node by type then use IF THEN

Post 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?
BuckXUK
Newbie
Posts: 16
Joined: Thu Mar 01, 2012 8:43 pm

Re: Filter XML node by type then use IF THEN

Post 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.
Regards,

William Buckingham
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: Filter XML node by type then use IF THEN

Post by abonsey »

Hi William,
Just PM'd you with a few questions.

Andrew
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: Filter XML node by type then use IF THEN

Post 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>
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Filter XML node by type then use IF THEN

Post 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']
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: Filter XML node by type then use IF THEN

Post by abonsey »

Hi,
Could you clarify "XML class within scripter". I've never done anything like this before.

Thanks
Andrew
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Filter XML node by type then use IF THEN

Post 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
Last edited by gabrielp on Mon May 09, 2016 7:00 pm, edited 1 time in total.
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: Filter XML node by type then use IF THEN

Post 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 :(
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Filter XML node by type then use IF THEN

Post 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']");
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: Filter XML node by type then use IF THEN

Post by abonsey »

Thanks for the help. All working great now.
Onwards and Upwards from here :D

Andrew
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: Filter XML node by type then use IF THEN

Post 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);
}
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Filter XML node by type then use IF THEN

Post 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.
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Post Reply