Skip to main content
Known Participant
September 1, 2023
Answered

To call xslt scripts developed inside ExtendScript

  • September 1, 2023
  • 2 replies
  • 2825 views

xsl script have been developped, so I would like to call xsl scripts developed inside ExtendScript in order the users don't need to save .FM as .xml, then, run xslt script to transfter the other .xml format. Is it possible? 

 

Thanks.

 

    Correct answer frameexpert

    Here is a general purpose function that you can use to invoke an XSLT transform:

     

    function runTransformation (transform) {
        
        /* transform is a JavaScript object:
           .xml = absolute path to the file to transform
           .xsl = absolute path to the XSL stylesheet
           .pro = XSLT processor (SAXON)
           .out = absolute path to the transformed file.
           .console = true to show the Console,
               false to suppress the Console
           .params = an array of objects containing
               name/value pairs (optional)
        */
        
        var cmd, count, i, result;
        
        // Create the command string.
        cmd = 'XSLTRunTrScenario ' +  
            '-file \"' + transform.xml + '\" ' +
            '-xslfilepath \"' + transform.xsl + '\" ' +
            '-processor \"' + transform.pro + '\" ' +
            '-outputfilepath \"' + transform.out + '\"';
            
        // See if Console messages should be displayed.
        if (transform.console === false) {
            cmd += " -dontusefmconsole";
        }
    
        // Add any parameters to the command line.
        count = transform.params.length;
        for (i = 0; i < count; i += 1) {
            cmd += ' -sparam ' + transform.params[i].name + ' "' + 
                transform.params[i].value + '"';
        }
    
        // Invoke the transform.
        result = CallClient ("FmXSLT", cmd);
    }
    

     

    2 replies

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    September 1, 2023

    Here is a general purpose function that you can use to invoke an XSLT transform:

     

    function runTransformation (transform) {
        
        /* transform is a JavaScript object:
           .xml = absolute path to the file to transform
           .xsl = absolute path to the XSL stylesheet
           .pro = XSLT processor (SAXON)
           .out = absolute path to the transformed file.
           .console = true to show the Console,
               false to suppress the Console
           .params = an array of objects containing
               name/value pairs (optional)
        */
        
        var cmd, count, i, result;
        
        // Create the command string.
        cmd = 'XSLTRunTrScenario ' +  
            '-file \"' + transform.xml + '\" ' +
            '-xslfilepath \"' + transform.xsl + '\" ' +
            '-processor \"' + transform.pro + '\" ' +
            '-outputfilepath \"' + transform.out + '\"';
            
        // See if Console messages should be displayed.
        if (transform.console === false) {
            cmd += " -dontusefmconsole";
        }
    
        // Add any parameters to the command line.
        count = transform.params.length;
        for (i = 0; i < count; i += 1) {
            cmd += ' -sparam ' + transform.params[i].name + ' "' + 
                transform.params[i].value + '"';
        }
    
        // Invoke the transform.
        result = CallClient ("FmXSLT", cmd);
    }
    

     

    Cheng-TAuthor
    Known Participant
    September 5, 2023

    Thanks  a lot for your general purpose code! now I got the "result = -1" which is better than before, returns something;-).

    The command used is shown below (without params (Optional)),

    <XSLTRunTrScenario
    -file "C:\temp\chapters_structured.xml"

    -xslfilepath "C:\temp\test.xsl"

    -processor "SAXON"

    -outputfilepath "C:\temp\chapters_structured_after.xml">

     

    Is the command wrong? by the way, what should be set for "sparam"? Is it possible to explain a little bit about this with the example?  Thanks.

     

     

     

     

    frameexpert
    Community Expert
    Community Expert
    September 5, 2023

    You want to put all of your XSLTRunTrScenario into a string variable (without the angled brackets in your example) and then pass it to this call:

    // Invoke the transform.
        result = CallClient ("FmXSLT", cmd);

    where cmd is your string variable.

    frameexpert
    Community Expert
    Community Expert
    September 1, 2023

    I suspect that Jang Graat will respond, but here is a link to one of his blog posts to get you started:

    https://blog.adobe.com/en/publish/2017/11/21/xslt-support-in-framemaker-2017

    If I have time later, I will post some example code. I use this feature extensively in my ExtendScript scripts to run XSLT stylesheets. FrameMaker has the Saxon Enterprise Edition built in and you can do a lot with it from your scripts.

    Cheng-TAuthor
    Known Participant
    September 1, 2023

    Thanks for your quick reply. 

    I will be really  appreciate if you have time to post some example code in order for me to understand better and hand on  it quickly. Thanks again.