1 Reply Latest reply on Sep 16, 2009 5:30 AM by nickbarreto

    [JS] XML import into xmlElement issue


      Hello Forum,


      This is my first foray into writing javascripts, so bear with me if I've got something wrong.


      I'm on Windows XP and using InDesign CS3 and the ExtentScript Toolkit to write my script. What I want the script to do (initially at least) is to open an InDesign file that has been already been formatted to take the XML file, and import the data, merging on the matching elements, thus creating a catalogue of books.


      Now I've got a template file already set up, and an XSL file that formats my XML to match up with the structure of the template. Everything works just fine when I'm importing the XML through InDesign manually. However, my XML structure is such that the parent element is <inventory>, and within that there is <BK>, and within that several instances of <book>, which has several elements that make it up. So what's happening is that when I'm importing manually in InDesign, I always have to make sure I select BK in the structure pane before I import, or else InDesign clones <BK> for each instance of <book>. This means that the xml doesn't match the elements (since the cloned BK has nothing in it, it is just brought it from the XML data, and not the BK with the tagged elements that exists on the InDesign file).


      So my question is: How do I select the already existing xmlelement BK in the document I've opened in my script? The Adobe documentation on JS tells me that I can import into selected xmlelement, and gives me this snipped of code which I've edited slightly since I know BK is the second xmlelement in that array. (Or I think I do...am I wrong on this?)


      var myXMLElement = myDocument.xmlElements.item(1);
      myDocument.xmlImportPreferences.importToSelected = true;
      myDocument.select (myXMLElement);



      When I try and run this I get an error, that myXMLElement is an invalid parameter for select, that it expected an object, or array of objects, etc, but received nothing. This is pretty much what the documentation tells me to do, but it isn't working.


      I've also tried selecting elementByName, which didn't work either. Anyone know what I'm doing wrong? It seems to me like it would be a simple thing to do, but I've hit a wall and can't seem to get anywhere. This is just the first part of the script, which I then want to autoflow through so the catalogue has as many pages as needed, and once that's done, to export to PDF for me, but I can't seem to get past this first hurdle.


      Once I've got this working, I may move on to setting up a UI so users can select from different templates and point to the right xml and xsl files, but that will be another battle.


      Many thanks in advance,

      Nick B

        • 1. Re: [JS] XML import into xmlElement issue
          nickbarreto Level 1

          I figured it out myself, after much frustration!


          So, for future reference, if anyone is having this problem, here is the explanation, and the correct code:


          The problem I was having was indeed with the selection from the array. The BK element was not the second element in the array at all. It was the first element within its parent element.


          so myDocument.XMLElements only had one item in it. The path that I wanted was:


          var myXMLElement = myDocument.XMLElements.item(0).XMLElements.item(0)


          I guess this is really basic, but it just took me a long time to get my head around this stuff, since it was my first real foray into scripting.