12 Replies Latest reply on Jul 22, 2008 9:53 PM by GagnonEric

    [JS][CS3] Trying to use XSL to transform a XML document

      I´m trying to do a script to format my XML in a particular way. The XSL document is ok because I´ve used this document to format an XML document in Indesign CS3 and works fine.However, when I´m trying to use this document in my scripting method I can´t obtain my XML formatted. I´ve tried it many times but I really don´t know where is the problem.

      I´ve changed the XMLExportPreferences (allowTransform,TransformFileName)in my scripting method before exporting my XML in order to obtain a formatted XML.
      Anyone have any idea? What are the steps to make this work?

      Thank u,
      Peter
        • 1. Re: [JS][CS3] Trying to use XSL to transform a XML document
          srakete Level 1
          Could you post your script ? You are doing an export, right ? Have you checked the path to your XSL in the script? Mac or PC ?

          Stefan
          • 2. Re: [JS][CS3] Trying to use XSL to transform a XML document
            Level 1
            I´m using these methods to get the path of the xsl and the path is the same for the script and for the XslDocument. I have a PC (Windows) and I´m using Indesign CS3 and the script is Javascript(ExtendedScript).

            The methods are:
            -------------------------------------------------
            function myGetScriptBasePath() {
            return myGetScriptPath().path;
            }
            function myGetScriptPath() {
            try{
            return app.activeScript;
            }
            catch(myError){
            return File(myError.fileName);
            }
            }
            -------------------------------------------------
            Firstly, I´m import the xml to my Indesign document, and after that I want to export the xml with a different format(using a Xsl document)

            The code of this part is:
            ---------------------------------------------------------------------
            app.xmlExportPreferences.allowTransform=true;
            var XSLFile=new File(myGetScriptBasePath()+"/DocumentNewFormat.xsl");
            app.xmlExportPreferences.transformFilename=XSLFile;
            var myXmlAuxFile =new File(myGetScriptBasePath()+"/FormattedDocument.xml");
            myDocument.exportFile(ExportFormat.xml, myXmlAuxFile);
            ---------------------------------------------------------------------
            • 3. Re: [JS][CS3] Trying to use XSL to transform a XML document
              srakete Level 1
              Peter,
              I have checked with your script and I get an XML Document, that has been transformed. So everything looks good. Are you not getting an XML File ? Have you checked the path of myXmlAuxFile with the databrowser of the ESTK ?

              Stefan
              • 4. Re: [JS][CS3] Trying to use XSL to transform a XML document
                Level 1
                I get a XML File but without the format indicated in the XSL document. I will check the path of the myXmlAuxFile if there is the problem.

                I will try again.

                Thank you,
                Peter
                • 5. Re: [JS][CS3] Trying to use XSL to transform a XML document
                  I'm having a similar problem but with importing XML. When I run this script I get a dialogue saying that "Stylesheet 'Macintosh HD:Users:myFolder:transform.xsl, specified in the XSLT processing instruction can not be found." But this is the location of the stylesheet??

                  Here's the entire script (a combination of Adobe samples):

                  main();
                  function main(){
                  mySetup();
                  mySnippet();
                  }
                  function mySetup(){
                  var myDocument = app.documents.add();
                  var myRootXMLElement = myDocument.xmlElements.item(0);
                  var myXMLTag = myDocument.xmlTags.add("body");
                  var myXMLElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag);
                  myDocument.select(myXMLElement);

                  //Sets XML import options.
                  var myXMLImportPreferences = myDocument.xmlImportPreferences;
                  myXMLImportPreferences.createLinkToXML = false;
                  myXMLImportPreferences.ignoreUnmatchedIncoming = false;
                  myXMLImportPreferences.ignoreWhitespace = true;
                  myXMLImportPreferences.importCALSTables = true;
                  myXMLImportPreferences.importStyle = XMLImportStyles.mergeImport;
                  myXMLImportPreferences.importTextIntoTables = false;
                  myXMLImportPreferences.importToSelected = true;
                  myXMLImportPreferences.removeUnmatchedExisting = false;
                  myXMLImportPreferences.repeatTextElements = true;
                  myXMLImportPreferences.allowTransform = true;
                  myXMLImportPreferences.transformFilename = File("/Users/myFolder/transform.xsl");
                  myDocument.importXML(File("/Users/myFolder/document.xml"))
                  }
                  function mySnippet(){
                  var myDocument = app.documents.item(0);
                  myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                  myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                  myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
                  myDocument.xmlElements.item(0).xmlElements.item(0).placeIntoFrame(myDocumen pages.item(0), [35, 35, 758, 576]);
                  • 6. Re: [JS][CS3] Trying to use XSL to transform a XML document
                    Level 1
                    Fellow Scripters,

                    Specifying a file for XSL transform on import does not work on the Mac OS--it's a bug. You'll have to do the transform outside InDesign, then import the transformed XML.

                    Sorry,

                    Ole
                    • 7. Re: [JS][CS3] Trying to use XSL to transform a XML document
                      If you are not aware of it: AppleScript users - there is a great freeware AppleScript addition available from Latenight Software (latenightsw.com). Although it is freeware, I would ask that you do Mark a favor and make a donation. He has saved our hides with this one. I haven't tried it with everything, but so far it has served my needs well.

                      Shirley Hopkins
                      AppleScripting InDesign CS (included CD includes scripts for CS2 and CS3)
                      • 8. Re: [JS][CS3] Trying to use XSL to transform a XML document
                        liedzeit Level 2
                        @Ole
                        Any chance this will be fixed before CS4? I need to change the "-" to "_" of all attribute names because it is interpreted by XTendScript Toolkit 2 as a minus (it seems).
                        Right now I do a change and replace within the script but it takes ages. I would prefer not to have to fall back to a non-InDesign/Script solution.

                        Ralf
                        • 9. Re: [JS][CS3] Trying to use XSL to transform a XML document
                          Dirk Becker  Level 4
                          Ralf, could you please give a sample that illustrates your original problem? And if it is not about XSL, let's take it into a separate thread.

                          This ...

                          var x = new XML("");
                          $.writeln(x.attributes().length());
                          $.writeln(x.attributes()[1].localName());
                          $.writeln(x.attribute("att-name").nodeKind());
                          $.writeln(x.attribute("att-name").toString());

                          ... yields this output:

                          2
                          att-name
                          attribute
                          q

                          Dirk
                          • 10. Re: [JS][CS3] Trying to use XSL to transform a XML document
                            liedzeit Level 2
                            Dirk, the problem is that this leads to an error:

                            var x = new XML("");
                            var attvalue=x[0].@att-name;
                            alert(attvalue);

                            whereas this works

                            var x = new XML("");
                            var attvalue=x[0].@att_name;
                            alert(attvalue);

                            That is why I wanted to change "-" to "_" by XSL. And I did. (Calling AppleScript that calls a shell script that calls saxon.)

                            But I just saw that I might be lucky using xpath.

                            var xpathexpr="//@att_name";
                            var g=x.xpath(xpathexpr);
                            alert(g.toString());

                            Thank you,
                            Ralf
                            • 11. Re: [JS][CS3] Trying to use XSL to transform a XML document
                              srakete Level 1
                              Hi,
                              if you are working with templates into which you want to import your XML, I have found a limited way
                              for using XSL transformations by script on the Mac.
                              Just import an XML manually with the XSL that you would like to apply to that document. Then delete the XML in the document that you imported and save the document as your template.
                              In your script you then use:

                              myDoc.xmlImportPreferences.allowTransform = true/false;

                              to activate or deactivate the XSL.
                              Works fine for me.

                              Stefan
                              • 12. Re: [JS][CS3] Trying to use XSL to transform a XML document
                                GagnonEric Level 1
                                "If you are not aware of it: AppleScript users - there is a great freeware AppleScript addition available from Latenight Software (latenightsw.com)."

                                On OSX, it's also possible to run a transformation using the shell and call of libxml2 (but i'm not fully sure if the libs are installed on every system or just with a developper install).

                                I have done this and it was working very well. I was using satimage XML OSAX to handle my xml files with applescript, but i had some problems with the XSLT transform with the osax (but i dont remember exactly what).