Extracting information from an XML Report file via XPath (9.1)

The pdfToolbox specific "app.doc.result.reports" object returns an array of reports that have been generated in a previous Process Plan step. It can be combined with file.read which would read an XML report into a string and to then convert that string back into an XML object with xml=new XML().

Then xml.registerNamespace allows for associating the XML Report namespace, which is "http://www.callassoftware.com/namespace/pi4" for pdfToolbox 9 XML Reports with a abbreviation.

Finally xml.path can be used to read information from the XML object via an XPath expression.

The example below extracts the information about what plates are used by a PDF file from the XML report and writes that information into a variable "text". In the Process Plan example which is attached to this article the value of this variable is then used in a later step to write that information onto all PDF pages.

//Get first report, assign it to "file", read it's content into a string and 
//convert that string into an XML object
app.vars.report = app.doc.result.reports[0];
var file = new File( app.doc.result.reports[0] );
var string = file.read();
var xml = new XML(string);

//Register the XML namespace with p
xml.registerNamespace("p","http://www.callassoftware.com/namespace/pi4");

//Get the list of platenames
app.vars.plates = xml.xpath( "//p:report/p:document/p:doc_info/p:platenames/p:platename/text()" );

//Write the list of platenames into a variable that is available throughout the execution context
app.vars.text = app.vars.plates;

0 Comments

Send Your Comment

E-Mail me when someone replies to this comment