Filter XML node by type then use IF THEN
Filter XML node by type then use IF THEN
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?
Can this be done via scripting?
Re: Filter XML node by type then use IF THEN
Hi Andrew
Yes, you can do all this with scripting. You'll just need the right XPath syntax to identify your target Extrinsic node.
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
William Buckingham
Re: Filter XML node by type then use IF THEN
Hi William,
Just PM'd you with a few questions.
Andrew
Just PM'd you with a few questions.
Andrew
Re: Filter XML node by type then use IF THEN
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>
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
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
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Re: Filter XML node by type then use IF THEN
Hi,
Could you clarify "XML class within scripter". I've never done anything like this before.
Thanks
Andrew
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
Do you have the scripting + XML modules? If not, we'll have to come up with a different solution.abonsey wrote:Hi,
Could you clarify "XML class within scripter". I've never done anything like this before.
Thanks
Andrew
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
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Re: Filter XML node by type then use IF THEN
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
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
I updated the link in my last post, that copied over wrong.
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.
Probably should read:
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.but it errors.... clearly it's wrong
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']);
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
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Re: Filter XML node by type then use IF THEN
Thanks for the help. All working great now.
Onwards and Upwards from here
Andrew
Onwards and Upwards from here

Andrew
Re: Filter XML node by type then use IF THEN
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);
}
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
What errors are you getting?abonsey wrote:here's what I've got so-far that doesn't work.
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'})
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'})
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix