Page 1 of 1

Merging XML jobtickets into one

Posted: Wed Sep 25, 2024 1:39 pm
by zurek
Hi guys!
I'm looking for a way to merge multiple (unknown number) of XML job tickets into one, right now.
Current XML template (using Create log for that):

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<!-- ============================================================================= -->
<!-- IHPS DFE "print-job" example XML -->
<!-- 5/15/2012 -->
<!-- release 2.5, spec number 11 -->
<!-- ============================================================================= -->
<!--  quality-mode-requested: standard, dual-drop or hybrid-k -->

<message>
   <target>jc</target>
   <message-name>print-job</message-name>
   <customer-job-id>[Job.PrivateData:Key="customer-job-id"]</customer-job-id>
   <job>
      <quantity>[Job.PrivateData:Key="qtyWithExtras"]</quantity>
      <frame-sort-order>collated</frame-sort-order>
      <interleave-pattern>AB</interleave-pattern>
      <rotate>90</rotate>
      <tumble>true</tumble>
      <frame-mm-along-web>[Job.PrivateData:Key="job_frame-mm-along-web"]</frame-mm-along-web>
      <frame-mm-across-web>[Job.PrivateData:Key="job_frame-mm-across-web"]</frame-mm-across-web>

      <rip>
         <color-setup-requested>[Job.PrivateData:Key="rip_color-setup-requested"]</color-setup-requested>
         <quality-mode-requested>[Job.PrivateData:Key="rip_quality-mode-requested"]</quality-mode-requested>
         <job-control>delete-after-printed</job-control>
      </rip>

      <paper>
         <type>[Job.PrivateData:Key="paper_type"]</type>
         <width-mm>[Job.PrivateData:Key="paper_width-mm"]</width-mm>
      </paper>
      <files>
         <file>
            <sequence>0</sequence>
            <filename>[Job.PrivateData:Key="file_filename"]</filename>
            <number-of-frames>[Job.PrivateData:Key="file_number-of-frames"]</number-of-frames>
         </file>
      </files>
   </job>
</message>
I want to merge XMLs into one, keeping all tags intact but aggregating all what's inside of <file> tag, for example:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<!-- ============================================================================= -->
<!-- IHPS DFE "print-job" example XML -->
<!-- 5/15/2012 -->
<!-- release 2.5, spec number 11 -->
<!-- ============================================================================= -->
<!--  quality-mode-requested: standard, dual-drop or hybrid-k -->

<message>
   <target>jc</target>
   <message-name>print-job</message-name>
   <customer-job-id>[Job.PrivateData:Key="customer-job-id"]</customer-job-id>
   <job>
      <quantity>[Job.PrivateData:Key="qtyWithExtras"]</quantity>
      <frame-sort-order>collated</frame-sort-order>
      <interleave-pattern>AB</interleave-pattern>
      <rotate>90</rotate>
      <tumble>true</tumble>
      <frame-mm-along-web>[Job.PrivateData:Key="job_frame-mm-along-web"]</frame-mm-along-web>
      <frame-mm-across-web>[Job.PrivateData:Key="job_frame-mm-across-web"]</frame-mm-across-web>

      <rip>
         <color-setup-requested>[Job.PrivateData:Key="rip_color-setup-requested"]</color-setup-requested>
         <quality-mode-requested>[Job.PrivateData:Key="rip_quality-mode-requested"]</quality-mode-requested>
         <job-control>delete-after-printed</job-control>
      </rip>

      <paper>
         <type>[Job.PrivateData:Key="paper_type"]</type>
         <width-mm>[Job.PrivateData:Key="paper_width-mm"]</width-mm>
      </paper>
      <files>
         <file>
            <sequence>0</sequence>
            <filename>[Job.PrivateData:Key="file_filename"]</filename>
            <number-of-frames>[Job.PrivateData:Key="file_number-of-frames"]</number-of-frames>
         </file>
        <file>
            <sequence>1</sequence>
            <filename>[Job.PrivateData:Key="file_filename"]</filename>
            <number-of-frames>[Job.PrivateData:Key="file_number-of-frames"]</number-of-frames>
         </file>
        <file>
            <sequence>2</sequence>
            <filename>[Job.PrivateData:Key="file_filename"]</filename>
            <number-of-frames>[Job.PrivateData:Key="file_number-of-frames"]</number-of-frames>
         </file>
      </files>
   </job>
</message>
Any idea how to achieve that?

My coding is super limited so I'm trying to do an XSLT transform with built-in configurator as well as with Saxon. I'm testing Saxon with some super simple xmls but results I'm getting are either unchanged xmls or warning "Processing failed with the following error: Input is a directory, but output is not". Should I send folder with xmls to Saxon anyway?

Re: Merging XML jobtickets into one

Posted: Wed Sep 25, 2024 5:06 pm
by tdeschampsBluewest
Hi there,
We are actually developping this, and it's under review on the appstore, it will be available soon ;)

In the meantime, here is a simple nodeJS script to use in switch.

Install nodeJS if not already installed
Create a file named merge.js, and copy paste this code :

Code: Select all

const fs = require('fs');
const path = require('path');

// Function to merge all files in a directory
const mergeFiles = (inputDir, outputFile) => {
    // Check if input directory exists
    if (!fs.existsSync(inputDir)) {
        console.error(`The input directory "${inputDir}" does not exist.`);
        process.exit(1);
    }

    // Get all the files in the directory
    const files = fs.readdirSync(inputDir);

    // Filter out any non-file entries
    const filePaths = files
        .map(file => path.join(inputDir, file))
        .filter(filePath => fs.lstatSync(filePath).isFile());

    // Check if there are any files to merge
    if (filePaths.length === 0) {
        console.error(`No files found in the directory "${inputDir}".`);
        process.exit(1);
    }

    let mergedContent = '';

    // Loop through each file and append its content
    filePaths.forEach(filePath => {
        const content = fs.readFileSync(filePath, 'utf-8');
        mergedContent += content + '\n'; // Add a newline between each file's content
    });

    // Write the merged content to the output file
    fs.writeFileSync(outputFile, mergedContent, 'utf-8');
    console.log(`Files merged successfully into "${outputFile}".`);
};

// Get the arguments from the command line
const args = process.argv.slice(2);
const inputDir = args[0]; // The directory containing the files to merge
const outputFile = args[1]; // The file to write the merged content

// Validate arguments
if (!inputDir || !outputFile) {
    console.error('Usage: node mergeFiles.js <input-directory> <output-file>');
    process.exit(1);
}

// Call the mergeFiles function
mergeFiles(inputDir, outputFile);
Then in switch, use Run command app
Set the command to :

Code: Select all

node.exe "path/to/merge.js"  "%%InputPath%%" "%%OutputPath%%.xml"
Send a folder to this app, and it will output a xml file concatened.

Re: Merging XML jobtickets into one

Posted: Thu Sep 26, 2024 10:24 am
by zurek
Thank you, seems to be working but of course it's just copy-paste-like merge with content of one xml after another. Will the app allow some more options like which tags should be merged?

Re: Merging XML jobtickets into one

Posted: Thu Sep 26, 2024 12:03 pm
by zurek
Hi again, I've actually managed to adjust the script with chat-gpt so only particular tags I'm intetested in are getting merged, remaining content is being kept as is. Thanks a lot for the script.

Also looks like, rather than legacy scripts, node.js is much easier to understand (and adjust) by the AI so I'll be aiming to use node.js from now on.
Looking forward to see your app anyway :D

Re: Merging XML jobtickets into one

Posted: Wed Oct 02, 2024 12:22 pm
by NEOSA
Hi zurek,

May be you can also have a look with an App available, Data Tool Kit :

https://www.enfocus.com/en/appstore/pro ... ta-toolkit

We currently use it for a couple of customers, the Join function might be use for your case.

Feel free to share with the developer of this App, Sean will help you to solve what you except.

Re: Merging XML jobtickets into one

Posted: Wed Oct 09, 2024 10:33 am
by tdeschampsBluewest
zurek wrote: Thu Sep 26, 2024 12:03 pm Looking forward to see your app anyway :D
Hi Zurek!
Great news, our app is out, and you can try it for free on the appstore.
Let us know if it suit you :)