replace xml values with switch values

Post Reply
kevinjel
Newbie
Posts: 8
Joined: Thu Jun 11, 2015 10:42 am

replace xml values with switch values

Post by kevinjel »

Fellow switchers,

i use switch with xslt to create jobtickets from given XML files. now i want to modify data in that XML in switch then write a new XML with the new data (so replace values inside the XML by the new data from switch).

the xml file is quite big, so i don't feel like loading everything into private data or into a dataset made by switch via switchclient (for example) since then i have to create a new XSLT, which everytime i update the jobticket xslt i also have to update a second XSLT which uses the private data items.
for example:
in the XML there is:
<print duplex="yes" sides_printed="4/4" sides_printed_text="dubbelzijdig full colour" color_space="CMYK" collate="" facing_print="" page_order="" divider_sheet=""/>

if i want to replace the value duplex to "no" and the sides_printed to "4/0" but keep everything else like it is...
How would one go around that, is there a load xml replace value X by Y then export metadata... or anything like that ^^. if things ain't clear let me know. hmm... thinking about it, maybe XSLT 2.0?

thanks in advance.

Kevin
sander
Advanced member
Posts: 228
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: replace xml values with switch values

Post by sander »

I would read the entire XML, replace whatever you want, write back to a new file.

Replace whatever value you want like I do with the lineBreaks over here;

Code: Select all

function jobArrived( s : Switch, job : Job )
{

		// Get propertyValue
		var privateDataKey = s.getPropertyValue('PrivateData');
		
		// Create file with Windows-1252 coding, needed for certain characters
		var readBody = File.read(job.getPath(), "Windows-1252");
		
		// Replace LF with CRLF, Prodist needs CRLF for correct parsing of e.g. addresses
		var replaceLineBreak = readBody.replace(/\n/g,"\r\n");
		
		// Put body in private data
		job.setPrivateData(privateDataKey, replaceLineBreak);
		
		job.sendToSingle(job.getPath());
}
Part of my playground:
- HP Indigo 10k, HP Indigo 7600's (full options), Highcon Euclid III, Zünd S3
- HP Production Pro 6.0.1, HP Production Center 2.5.1 beta, Apogee 9.1, Enfocus Switch 13u1 & PitStop Server 13u2.

Chat: open-automation @ gitter
kevinjel
Newbie
Posts: 8
Joined: Thu Jun 11, 2015 10:42 am

Re: replace xml values with switch values

Post by kevinjel »

Sander, thx for the response :D :D

i don't really get what is happening in these steps;
1. in this step it loads the complete given XML:?: so i should set my xmlpickup with the name: "PrivateData" or...
2. creates a file
3. replaces the (old str,new str) but with the file just created in step 2 which is new created file so nothing to replace:?:
4. create the new private data, (old data, new data) :?:
5. write the file?

Probably a newbie question... but i can't wrap my head around it. Soz :roll:. don't got it working so hope this clears my confusion.

already big thanks.
Kevin
sander
Advanced member
Posts: 228
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: replace xml values with switch values

Post by sander »

Haha fair enough. I just copied this script as reference/idea for the replace part.

Just tested and came up with this one.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	// Read properties	
	var oldValue1 = s.getPropertyValue('oldValue1');
	var newValue1 = s.getPropertyValue('newValue1');	
	
	// Read incoming XML
 	var oldXML = File.read(job.getPath(), "Windows-1252");
	
	// Create new XML
	var newXML = job.createPathWithName(job.getName());
	
	// Replace stuff
	// These properties can easily be filled via standard Switch properties, see screenshot.
	var newXMLcontent = oldXML.replace(oldValue1,newValue1);
	
	// Write new XML
	File.write(newXML, newXMLcontent, 'Windows-1252');
	
	// Send newXML to output
	job.sendToSingle(newXML);	
}
Image

oldXML input:

Code: Select all

<print duplex="yes" sides_printed="4/4" sides_printed_text="dubbelzijdig full colour" color_space="CMYK" collate="" facing_print="" page_order="" divider_sheet=""/>
newXML output:

Code: Select all

<print duplex="yes" sides_printed="Hooray" sides_printed_text="dubbelzijdig full colour" color_space="CMYK" collate="" facing_print="" page_order="" divider_sheet=""/>
Part of my playground:
- HP Indigo 10k, HP Indigo 7600's (full options), Highcon Euclid III, Zünd S3
- HP Production Pro 6.0.1, HP Production Center 2.5.1 beta, Apogee 9.1, Enfocus Switch 13u1 & PitStop Server 13u2.

Chat: open-automation @ gitter
kevinjel
Newbie
Posts: 8
Joined: Thu Jun 11, 2015 10:42 am

Re: replace xml values with switch values

Post by kevinjel »

I'm not a programmer (i can python a tiny bit) so thats why i asked, i kinda thought it could work the way it was posted :lol: ...

but that work likes a charm! thank you very very very much.
So many possibility's. :geek:

Thanks alot!
sander
Advanced member
Posts: 228
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: replace xml values with switch values

Post by sander »

If you have the scripting module, the sky is the limit :D

Good to know it helped you out!
Part of my playground:
- HP Indigo 10k, HP Indigo 7600's (full options), Highcon Euclid III, Zünd S3
- HP Production Pro 6.0.1, HP Production Center 2.5.1 beta, Apogee 9.1, Enfocus Switch 13u1 & PitStop Server 13u2.

Chat: open-automation @ gitter
Post Reply