amend XML file [solved]

Post Reply
matthias
Newbie
Posts: 4
Joined: Wed Feb 15, 2017 10:01 am

amend XML file [solved]

Post by matthias »

Hi together,

I wonder if you can help me with the Scripting in Enfocus Switch.
I'm desperatly trying to change a value in an xml file.

there might be many different solutions in the switch universe to achieve something comparable. But in my case the xml and loaded metadata from the xml come into the script anyway. (downloading attached PDF assets)
what i want to do is to check if value has a certain value and if true change that value.

XML Path is for example
order->shipment->shipTo->carrier
if value is "dhl .+ national" i want to change it to "ups"

I found how to open the actual xml file

Code: Select all

var xmlPath = job.getPath();
var doc = new Document(xmlPath);
as well as save it and return it from the script

Code: Select all

doc.save(xmlPath);
job.sendToData(1, xmlPath, job.getName());
but the in between is a bit of magic.

Code: Select all

var root = doc.getDocumentElement();
but then all the getChildNodes and so on.
what would it look like to read the text value of one node and how to change a text value.

many thanks in advance
Matthias
Last edited by matthias on Tue Mar 07, 2017 5:03 pm, edited 1 time in total.
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: amend XML file

Post by sander »

If you are lazy like me and don't want to use the getChildNodes stuff you might want to use this:
viewtopic.php?f=12&t=1276&p=4354#p4352
matthias
Newbie
Posts: 4
Joined: Wed Feb 15, 2017 10:01 am

Re: amend XML file

Post by matthias »

Hey Sander, thanks a lot - i have set this one up successfully following your code snipped.


With mixed feelings i'll push this into my production workflow.
Would future sleep better if it was not the lazy solution but the accurate one.
since i used tags and part of the text value, i'm pretty sure it will only replace the correct part within the xml.

for bigger xml files it seems to be a fragile solution depending what one intends to do.


maybe someone still could help me with the correct use of the DOM structure/ child nodes and access text values.

thanks, Matthias
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: amend XML file

Post by sander »

I agree. I also have a case which require a sleep-better-solution ;)

I think this script can point you in the right direction. This one is used to change the imposition name in a HP jdf.
All credits to Chad, HP, he created it.

Code: Select all

function jobArrived( s : Switch, job : Job )
function jobArrived( s : Switch, job : Job )
{
	var jdfDoc = new Document(job.getPath());
	var jdfNS = jdfDoc.createDefaultMap();
	var jdfLoc = job.createPathWithName(job.getName());
	
	var fileSpec = jdfDoc.evalToNode("//jdf:JDF/jdf:ResourcePool/jdf:LayoutPreparationParams/jdf:ExternalImpositionTemplate/jdf:FileSpec", jdfNS);
	if (fileSpec != null) {
		var urlVal = "urn:" + s.getPropertyValue("ImpositionTemplate");
		s.log(-1, "Adding URL attribute to ExtneralImpositionTemplate/FileSpec element with value of: \"" + urlVal + "\"");
		fileSpec.addAttribute("URL", jdfNS, urlVal);
	}
	jdfDoc.save(jdfLoc);
	job.sendToSingle(jdfLoc);
}
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

Re: amend XML file

Post by cstevens »

Once you have the file opened as a DOM you can use:

Document.evalToString() / .evalToNumber() / evalToBoolean() functions to get the data you want:

Code: Select all

var isbn = doc.evalToString("/Books/Book/Publisher/@ISBN", null);
If you search in the Switch Scripter help for "Evaluating XPath" there's some good examples.

If you need to replace a value you typically need to find the parent node so that you can add an attribute with the same name or text value. Something like this:

Code: Select all

var publisher = doc.evalToNode("/Books/Book/Publisher", null);
publisher.addAttribute("ISBN", null, "NewValue");
Once you get familiar with the XPath format it gets really easy to do this stuff on complex XML files. It also allows the structure of the XML file to change and still allow you to get to the same data as long as it's XPath location doesn't change.

Namespaces complicate this whole thing however. The example that was shown earlier is a JDF ticket that has a namespace which affects all of the XPath expressions and requires you to create a map. If you're willing to post an XML sample it would help, otherwise you can use the XML pickup element to capture a sample and use the built-in path generator in switch to have Switch generate the XPath for you.
matthias
Newbie
Posts: 4
Joined: Wed Feb 15, 2017 10:01 am

Re: amend XML file

Post by matthias »

Hi guys,

thanks for all your input!
After the first shot with a plain replace on the actual text file it took me a while to get back to this.
I wanted a solution foolproof for other occasions where a plain replace might change more than the value i jsut thought of.

here's what i came up with:

Code: Select all

	// some properties to use it flexible within switch
	var dataset = s.getPropertyValue('dataset', job);
	var xpath = s.getPropertyValue('xpath', job);
	var tagvalue = s.getPropertyValue('tagvalue', job);
	
	// XML Dataset (dataset = "xml")
	var databaseDataset = job.getDataset(dataset);
	// XML Document
	var theXMLDoc = new Document(databaseDataset.getPath());	
	// Node (xpath = "/data/farbigkeit")
	var nodeByPath = theXMLDoc.evalToNode(xpath, null);
	
	//// change XML-tag value	
	// create new value (tagvalue = "2")
	var text2 = theXMLDoc.createText(tagvalue);
	//replace first of node with new text
	nodeByPath .replaceChild(text2, nodeByPath .getFirstChild());
		
	// Save the file
	theXMLDoc.save(databaseDataset.getPath());

	// finally output the job with its metadata
	job.sendToSingle(job.getPath(), job.getName());
instead of changing the XML Tag Value one can easily change the Attribute as well.

Code: Select all

	// change attribute "id" to value "12"
	nodeByPath .addAttribute("id", null, "12");
maybe this helped someone else coming across this thread.
i had a tough time to work me through all this as the manual and helppages from enfocus are really rare of examples.

have a good time,
matthias
Post Reply