Page 1 of 1

using metadata in a new file

Posted: Thu Aug 04, 2016 1:29 pm
by abonsey
I have a JDF (created via the HP JDF Control configurator) that has a dataset attached to it.
I'm trying to create a simple text file for use elsewhere but I keep getting the error:

Error in line 11 of script : TypeError. 'ToString' undefined or not a function
Invalid XML document


Below is the script. Can anybody see what I've done wrong??


function jobArrived( s : Switch, job : Job )

{
var dataset = job.getDataset("XML");
var xml = new Document(dataset);
var JobCode = dataset.ToString("/Job/@JobCode")



var textFile = new File(s.createPathWithName(JobCode));
textFile.open(File.WriteOnly);

textFile.writeLine("Add to Digital");
textFile.writeLine("Indigo");

textFile.close();

job.sendToSingle(textFile);
job.sendToSingle(textFile.fullName);

}

Thanks
Andrew

Re: using metadata in a new file

Posted: Thu Aug 04, 2016 2:58 pm
by freddyp

Code: Select all

var dataset = job.getDataset("XML");
So far, so good.

Code: Select all

var xml = new Document(dataset);
To load an XML document you have to specify the path to the XML document, but you are using the Dataset object returned by getDataset as is. You have to use the backing file of the dataset: dataset.getPath( );

Code: Select all

var JobCode = dataset.ToString("/Job/@JobCode")
There are several functions for evaluating XPaths, but the one you need is evalToString. The function evalToString function also has to be done on the XML object, not on the Dataset object. And there should be a semicolon at the end of the line. The correct line is:

Code: Select all

var JobCode = xml.evalToString("/Job/@JobCode");

Re: using metadata in a new file

Posted: Fri Aug 05, 2016 11:46 am
by abonsey
Hi Freddy,
Thanks for that, but I must be missing it still as the changes still error. Here's what I've got:

var dataset = job.getDataset("XML");
var xml = new Document(dataset.getPath());
var JobCode = xml.evalToString("/Job/@JobCode");


var textFile = new File(s.createPathWithName(JobCode));


I still get the same error
Error in line 11 of script : TypeError. Undefined member function 'ToString' for object 'undefined' of type: 'Undefined'


Any ideas
Andrew

Re: using metadata in a new file

Posted: Mon Aug 08, 2016 10:33 am
by freddyp
As I do not know which of the lines is line 11 it is hard to say anything conclusive.

Are you sure the name of the dataset is "XML"? Add

Code: Select all

s.log(1,dataset);
after the line where you initialize the dataset variable to see if what you get is a Dataset object or undefined. My guess is that you get undefined in which case xml will also be undefined and evalToString does not work. But you should see other errors as well as the XML file cannot be loaded.

Are you testing this in Switch Scripter or in a flow? When in a flow, do not forget to stop and start the flow again or your changes are not taken into account.

Re: using metadata in a new file

Posted: Mon Aug 08, 2016 1:28 pm
by abonsey
I checked the dataset name and it is "XML".
After adding in the log command I get a warning about an invalid XML document.

I decide to export the XML to a new dataset and get the script to read that newly created dataset but it still errors.
All this is done within the flow and it is being stopped/started each time

Re: using metadata in a new file

Posted: Mon Aug 08, 2016 3:17 pm
by freddyp
Then you should investigate if the dataset with the name "XML" is actually an XML file. Go to the application data root folder (you find the path in the Preferences) and based on the time of the file you should be able to find the dataset backing file quite easily in the folder "datasets".

How is the dataset added?

Re: using metadata in a new file- SOLVED

Posted: Wed Aug 10, 2016 10:54 am
by abonsey
All working fine now. The amends to the script didn't take in the flow even after stop starting... came back to it an our later and it all processed OK with no further changes.

No on to something new :)