2 Replies Latest reply on Dec 5, 2014 1:31 AM by Dirk Becker

    Prepend XMLElements and adding children?

    Stamm Level 1

      I didn't know how to exactly write my question, as I have two different ones.

       

      Firstly:

       

      Is it possible to prepend XML Elements? I am adding xml elements via a custom panel, and at the end of the script, the rest of the elements are being added. But I need the elements which are added at the end to be prepended.

       

      Secondly:

       

      Why does the following snippet not work(I have minimised it to the essential code)?

       

      with(rootXmlElement){

           newElement = xmlElements.add('carl', yourSelection);

           newElement.xmlElements.add('newXMLElement');

      }

       

      While this:

       

      with(rootXmlElement){

           newElement = xmlElements.add('carl');

           newElement.xmlElements.add('newXMLElement');

      }

       

      works? I figured, that when you link the xmlElement to some object, the Element is somehow different, that it has no "xmlElements.add" anymore. But I just can't find a solution to this.

       

      Any help is greatly appreciated. If someone has, I would love a great documentation about XML in indesign scripting, as the official scripting tutorial is not offering really much to it.

        • 1. Re: Prepend XMLElements and adding children?
          JulianFT Level 1

          For your second question, although I have not found a solution to this problem myself either, I've recently been struggling with the same.

           

          What (I think) I've found so far is that the difference lies in whether an XML element is placed in the document (as, in your case, it would be when you use 'yourSelection'), as opposed to a parent and child element that both only appear in the XML structure. I think this has something to do with an element automatically being added to a parent Story element when placed in the document, and that screwing up the structure in some way. It's just basic symptom diagnosis though, there's probably someone with a much more logical explanation for all this.

          • 2. Re: Prepend XMLElements and adding children?
            Dirk Becker Level 4

            Sounds like a bug to me - especially as the add() returns NULL rather than throwing an error.

            A workaround could be something like this:


            if( xe.parentStory instanceof XmlStory ) {

              x1 = xe.parent.xmlElements.add("x1");

              x1.move(LocationOptions.BEFORE,xe);

            } else {

              x1 = xe.parent.xmlElements.add("x1",xe.storyOffset);

            }