12 Replies Latest reply on Jun 14, 2010 3:57 AM by Harbs.

    [JS-CS4] Associated XML element...

    Emanuele Radaelli Level 1

      Hi,

      I'm going crazy with this.

      I have a table built with XML import and I need to get the XML element associated with a cell.

      I use this code:

       

      cell.associatedXMLElement

       

      but it always returns "null".

      Obviously that cell has a content in XML structure...

      Any idea why?

       

      Thanks.

        • 1. Re: [JS-CS4] Associated XML element...
          Dirk Becker Level 4

          How do you get your cell? This works for me:

           

          $.writeln(app.activeDocument.textFrames.item(0).tables.item(0).cells.item(0).associatedXML Element.markupTag.name);

          Dirk

          • 2. Re: [JS-CS4] Associated XML element...
            grefel Level 2

            no indesign to test around here.

             

            there is a similar problem with characters/texts which are not positioned in the layout view. indesign can only access the associatedXMLElements property when the xml is placed. could be the same with cells!

             

            this does not work:

            xmlElement.characters[10].associatedXMLElements[0]

            (by the way this makes sense if you check for child elements for a certain range)

             

            gregor

            • 3. Re: [JS-CS4] Associated XML element...
              Emanuele Radaelli Level 1

              Here's my script:

               

              var my_doc = app.activeDocument;

              var my_table = my_doc.pages.firstItem().textFrames.firstItem().tables.firstItem();

              my_table.columns.firstitem().select();

              var my_selection = doc.selection;

              for(var c = 0; c < my_selection.length; c++){

                   if(my_selection[c].associatedXMLElement.xmlAttributes.itemByName("type").value != "house"){

                        my_selection[c].appliedCellStyle = doc.cellStyles.itemByName("column");

                        my_selection[c].clearCellStyleOverrides(true);

                   }

              }

               

              This will run once the table is placed through XML import

              I need to check the "type" XML attribute value for every XML cell, that's why I'm performing that if statement.

              Basically if a cell's XML element has a "type" attribute with "house" value the script will not apply the "column" style.

              The problem is that

              my_selection[c].associatedXMLElement

              always return null.

              The XML data structure is:

              <List><Category><Table><Cell/><Cell/>....</Table></Category></List>

              So I have a simple table inside a textFrame.

               

              I know what I'm doing could sound weird, but I experienced it's the most quick way to handle a big table within ID.

              Here's the background story

              Thanks again.

              • 4. Re: [JS-CS4] Associated XML element...
                grefel Level 2

                looks like a bug.

                 

                my_doc.pages.firstItem().textFrames.firstItem().tables.firstItem().associatedXMLElement;

                 

                works fine.

                 

                i think you do not need a selection (which looks llike a performance killer):


                var my_doc = app.activeDocument;


                var _tableXML= my_doc.pages.firstItem().textFrames.firstItem().tables.firstItem().associatedXMLElement;;


                for(var c = 0; c < _tableXML.xmlElements.length; c++){


                     if(_tableXML.xmlElements[c].xmlAttributes.itemByName("type").value != "house"){


                        _tableXML.xmlElements[c].applyCellStyle (my_doc.cellStyles.itemByName("column"), true);


                     }


                }

                • 5. Re: [JS-CS4] Associated XML element...
                  Emanuele Radaelli Level 1

                  Thanks for your suggestions Grefel.

                  I'm testing all kinds of approach to format an XML table and I have to say that the selection method is the one that require less time when it comes to applying cell styles.

                  Basically I do the following:

                  1.     I import the XML (as you wisely suggested me) without applying styles (2 minutes);

                  2.     Once the table is placed I run a script to select every column and apply a style to all the cells inside the selection (3 minutes);

                  3.     I run another script that adapt culmns width to their contents (15 minutes).

                   

                  Consider that I'm working with a table with 25460 cells and to loop with a FOR cycle through such many items takes a lot of time (I already tested and it tooks 5 hours).

                   

                  By now this looks like the most efficent solution in therms of time and performance.

                  I just have to solve that XMLElement issue...

                   

                  Thanks again.

                  • 6. Re: [JS-CS4] Associated XML element...
                    grefel Level 2

                    ok. sounds interesting...

                     

                    solving the associatedXML problem:

                     

                    _selection[c].texts[0].associatedXMLElements[0]

                     

                    works around the bug, if you've got inline elements you have to check for this.

                    • 7. Re: [JS-CS4] Associated XML element...
                      Emanuele Radaelli Level 1

                      The problem is that when you select a column the first item in the selection object is not an array of cells but an array of columns.

                      So within the selection if you want to reach a single cell you need to use this expression:

                      my_selection[0].cells[c]

                      where c is the cell index.

                       

                      my_selection[0].cells[c].associatedXMLElement

                      returns the XML element.

                       

                      Hope this will help.

                      Bye.

                      • 8. Re: [JS-CS4] Associated XML element...
                        Dirk Becker Level 4

                        You can save some loop iterations if you use the collection feature of InDesign.

                        Basically this way you can assign the same value to a whole set of cells with one command:

                         

                        myTable.columns.item(0).cells.everyItem().appliedCellStyle = redCellStyle;

                         

                        Use that approach for the majority, and only run a loop* across the few exceptions.

                        *Unfortunately evaluateXPathExpression() yields an array, not a collection.

                         

                        var rootXE = doc.xmlElements.item(0);
                        var houseXEs = rootXE.evaluateXPathExpression('//Cell[@type="house"]');
                        for( var i=0; i< houseXEs.length; i++ )
                              houseXEs[i].xmlContent.appliedCellStyle = null;

                         

                        Dirk

                        • 9. Re: [JS-CS4] Associated XML element...
                          Emanuele Radaelli Level 1

                          Thanks for your suggestion Dirk but I came to the conclusion that when it comes to apply styles to such a huge table it's quicker to use the selection method. From my test I have to say it's about 20 or 30 times faster and, you know, I'm working with a 600 rows table so...

                          • 10. Re: [JS-CS4] Associated XML element...
                            Harbs. Level 6

                            You can try using the x/y name of the cell rather thanresolving the reference. I believe it's much faster...

                             

                            Harbs

                            • 11. Re: [JS-CS4] Associated XML element...
                              Emanuele Radaelli Level 1

                              Sorry Harbs, but what do you mean by "using the x/y name of the cell" exactly?

                              • 12. Re: [JS-CS4] Associated XML element...
                                Harbs. Level 6

                                Select a cell and run this:

                                 

                                alert(app.selection[0].name);

                                 

                                It'll display the cell coordinates.

                                 

                                You can use table.cells.itemByName(coordinates), to specify a cell.

                                 

                                Of course tables with merged cells and columns can get a bit tricky...

                                 

                                Harbs