Page 1 of 1
					
				Injecting files using a Script_SOLVED
				Posted: Thu Feb 18, 2016 1:55 pm
				by victor Lukenga
				Hello, 
Im trying to inject files via my metadatas I have some issues
when I use the built-in inject tool I can import only the first file (I don't know if my XML is well writed ) when I choose the "files" node switch don't seem to read through the entire node
 
Code: Select all
	
	Xml:	
		<files>
			<file>
				<path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx1.pdf</path>
				<path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx2.pdf</path>
			</file>
		</files>
That's why I  turned myself to scripting:
Code: Select all
function jobArrived( s : Switch, job : Job )
{
	
var dataset = job.getDataset('Xml');
//var path = dataset.evalToString('/order/files');
var dummyPath =( 'C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxxx/ORxxxxxxx/docs/xxxxx1.pdf'); // for testing
job.sendToSingle(dummyPath);
}
 
The issues  are :
the injected file is cleared from his original location(we have to keep the customers files )
the original name and extention are replaced or disapear
Only one file is imported
I can figure this out hope you will help me 
Best regards 

 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 2:01 pm
				by sander
				You can't loop through nodes in Switch. Do you generate the XML by yourself?
Than the easiest approach is to create one XML for each file, which you can easily use with inject job.
Code: Select all
      <files>
         <file>
            <path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx1.pdf</path>
         </file>
      </files>
 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 3:17 pm
				by victor Lukenga
				Hi , thank you to take time to answer I really appreciate it
these Xmls are generated by a home made app  
one xml per file is a bit complicated, cause all files contained in the "docs" folder are part of one order : we may have e.g 2 files for one business card 
(fileRecto.pdf fileVerso.pdf)
 the best way for me will be to loop through "files" node. I've tried and spend few hours to find a solution  : answer is close to me but where 

 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 3:22 pm
				by sander
				I used to do it this way, without scripting: 
http://forum.enfocus.com/viewtopic.php?f=12&t=1201
Since Switch 13 I upgraded to the Saxon configurator: 
http://forum.enfocus.com/viewtopic.php?f=12&t=1237
So it's already done without scripting. Keep it simple 

 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 3:41 pm
				by victor Lukenga
				Yes this is my keyword too "keep it simple"
Ive just tried to run the saxon configurator but it return this 
Code: Select all
Flow validation error: The application is not available on Switch Server
?
I also donwload the version you provided on you post
I guess that saxon is open source ?
 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 3:56 pm
				by sander
				Running on Windows too?
Did you restart Switch / search for applications?
And yes, it's open source.
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 4:04 pm
				by victor Lukenga
				Yes I'm on windows 
I unistall /reinstall it : Strange I keep have the same error :/
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 4:08 pm
				by sander
				My Switch server is running on Windows Server 2012 R2 with only .NET Framework 4.5 installed, no 3.5 or earlier versions.
4.5 is not installed by default, do you have it?
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 4:17 pm
				by victor Lukenga
				[quote="sander"]My Switch server is running on Windows Server 2012 R2 with only .NET Framework 4.5 installed, no 3.5 or earlier versions.
Edit : I rebooted my machine : the configurator is now working
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Thu Feb 18, 2016 6:42 pm
				by victor Lukenga
				I wondered if evalToNodes can be suitable for my needs ?
Code: Select all
function jobArrived( s : Switch, job : Job )
{
var dataset = job.getDataset('Xml');
	
var xml = new Document(dataset);
var node = xml.evalToNodes('/order/files/file');
if(dataset != null){
	
	
	for (var i=0; i < node.length; i++) {
		var myNode = node.getItem(i);
    	var files = myNode.evalToString(".");
		
		job.log(2, "files are "+theNode.evalToString("."));
		job.sendToSingle();}
}else{}
}
 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Fri Feb 19, 2016 8:59 am
				by sander
				Yes this can be done, however I personally haven't done this yet. On the list to figure out later, little busy 
But I'm sure some other more experienced scripting forum members can help you out.
Curious, did you get it to work with the Saxon transform?
 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Fri Feb 19, 2016 2:13 pm
				by victor Lukenga
				thank you for your answer 
I tried to play with Saxon but honestly I don't have experience in XSLT so I didn't understand how to setup Saxon.
 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Fri Feb 19, 2016 3:34 pm
				by sander
				Based on your given XML example I would do this 
Configure Saxon like this;
With this as content of your victor.xsl:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="files/file/path">
        	<xsl:variable name="InputFile" select="base-uri()"/>
			<xsl:variable name="OutputFile" select="tokenize($InputFile,'/')[last()]"/>
        	<xsl:variable name="OutputNoExt" select="substring-before($OutputFile,'.')"/>
	    	<xsl:result-document href="{$OutputNoExt}_{format-number(position(),'000')}.xml" method="xml">
			<xsl:copy-of select="current()"/>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Your input XML...
Code: Select all
<files>
	<file>
	<path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx1.pdf</path>
	<path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx2.pdf</path>
	</file>
</files>
... will result in this two output XML's:
jobname_001.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx1.pdf</path>
jobname_002.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<path>C:/Users/flprint-switch/Desktop/checker/ORDxxxxxxx/ORDxxxx/docs/xxxxx2.pdf</path>
Does that help?
 
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Fri Feb 19, 2016 7:04 pm
				by victor Lukenga
				that's great 
Ill give a try this week end  I will also give you a feedback
Once Again Thank you for your help !
			 
			
					
				Re: /!\ Injecting files using a Scripting
				Posted: Mon Feb 22, 2016 3:23 pm
				by victor Lukenga
				Hi Sander,
Thanks a lot for your help! it works like a charm it was exaclty what I needed!! wonderful forum !! 
  
  
  
 .
thank you again !