5 Replies Latest reply on Sep 5, 2012 9:43 AM by dremoussenard-actcs

    Open XML and using xslt with JavaScript

    still_und_leise

      Hello,

      I want the user to be able to choose an XML-file. This XML-file should not be placed in InDesign, there is not even a document needed for my script. All I need is a variable, containing the file. However, the XML-file should be transformed with xslt into another XML-file. How do I do this?

       

      This is how I open the first XML-file, the one that should be transformed:

       

         var xmlDoc = File.openDialog ("Choose XML");
         if (xmlDoc == null) {return;}
          xmlDoc.open("r");
          xmlDoc = xmlDoc.read();

          xmlDoc = new XML(xmlDoc);


      I can adress different nodes and use xmlDoc as an XML-file, so that's fine, but how do I transform it?

       

       

      I read the Scripting Guide and tried this:

       

          var myDocument = app.documents.add();

          var myXMLImportPreferences = app.xmlImportPreferences;

          myXMLImportPreferences.transformFilename = "c:\myTransform.xsl";

          var xx = myDocument.importXML(File.openDialog ("XML"));

       

      However, it's not exactly what I want since I have to create a document and it's not working either, since xx stays undefined.

       

       

      Thanks for your help.

        • 1. Re: Open XML and using xslt with JavaScript
          John Hawkinson Level 5

          You can't get exactly what you want.

           

          Your first code example uses the InDesign JavaScript interpreter's XML support, called E4X. It has absolutely nothing to do with the InDesign document object model or InDesign's internal support for XML. It exists only within the JavaScript interpreter. It does not support XSLT transformations.

           

          Your second example leverages InDesign's built-in XSLT support (I'm not too sure how this works...); but that is only useful when importing XML into

          InDesign, which, as you have learned, requires a document.

           

          In the first example, you could call out to an external invokation of xsltproc and read in the input. How to do that differs for Windows and Mac.

           

          In the second example, you could export the XML from your Document back out to an XML file and read it back in with E4X.

           

          None of this is very appealing. What do you really need to do?

          • 2. Re: Open XML and using xslt with JavaScript
            still_und_leise Level 1

            Thanks for your answer.

             

            I want to find out if some xml-elements are part of the transformed xml-file. I don't want the users to transform the file with some editor themself, because I'm afraid this might confuse them.

            If one of the searched xml-elements is part of the transformed xml-file, it's written into a txt-file. However, it would propably be easier to work with the second idea, considering that each xml-element will later be translated into a paragraph style, which can be added to a new document. So basically one could say, I want to create a template based on the transformed xml-file. Also I'm not sure, which os will be used later, I'm working with windows right now.

            • 3. Re: Open XML and using xslt with JavaScript
              dremoussenard-actcs Level 1

              I'm trying to import xml, with a xsl transformation.

              And I'm using something similar to you : fixing the xmlimport preferences, etc

              Importing by the menu works. Importing with javascript doesn't work. I can see my xml file imported, but without any modification (described in the xsl file).

               

              Is it possible to import xml files, with xslt tranformation through a javascript script?

               

              Thank you

              Didier

              • 4. Re: Open XML and using xslt with JavaScript
                love_all Level 1

                Hi,

                 

                After impoting the xml try this!!!

                 

                 

                myDocument.xmlImportPreferences.allowTransform = true;

                myDocument.xmlImportPreferences.transformFilename = File("........................");\\give your xslt path

                myDocument.xmlImportPreferences.importStyle = XMLImportStyles.MERGE_IMPORT;

                myDocument.xmlImportPreferences.repeatTextElements = true;

                myDocument.xmlImportPreferences.importCALSTables = true;

                myDocument.xmlImportPreferences.importTextIntoTables = true;

                myDocument.xmlImportPreferences.removeUnmatchedExisting = false;

                 

                But make sure your xslt should be xml version="1.0"

                 

                 

                Thanks,

                love_all

                • 5. Re: Open XML and using xslt with JavaScript
                  dremoussenard-actcs Level 1

                  Thank you,

                   

                  I tried that.

                  But before or after, no modifications...

                  And the xsl file is ok (I can import "by hand" with the menu import xml).

                  Arg