4 Replies Latest reply on Nov 16, 2012 7:55 AM by bduffy323

    adding xmlattribute issue

    bduffy323 Level 1

      I thought this was going to be pretty straight forward but I cannot figure out why my attributes are not being set correctly. I am going through all page items and trying to find a particular xmlElement markupTag. When I find that tag I want to add an attribute to that xmlElement. My problem is when I find a second (or more) page item with an xmlElement with the same markup tag, I add the xmlAttribute to this element and the first one I set goes away, leaving only the last xmlElement encountered with the attribute. Here is the code I wrote. The key to testing this is to have at least two page items that have text tagged with the same markUp tag. Place that markUp tag in the "tagToMatch" in the findTag method call. Any help would be appreciated. Thanks!

       

       

       

      var allPageItems = app.activeDocument.allPageItems;
      
          for (var i = 0; i < allPageItems.length; i++) 
          {
              var myPageItem = allPageItems[i];
      
      
              var xmlElements = findTag(myPageItem,"tagToMatch");
              for (var j = 0; j < xmlElements.length; j++) 
              {
                  var myXmlElement = xmlElements[j];
                  var myAttribute = myXmlElement.xmlAttributes.item("groups");
                  if (myAttribute.isValid)
                  {
                      var myGroups = myAttribute.value;
                      //do more logic here
                  }
                  else
                  {
                      myXmlElement.xmlAttributes.add("groups","uniqueGroupName");
                  }
              }  
      
      function findTag(myItem,myTag)
      {
      
          if (myItem.getElements()[0].constructor.name == "TextFrame")
          {
              var temp = myItem.parentStory.associatedXMLElement;
              if (temp==null) return [];
      
      
              var arrayOfMatches = myItem.parentStory.associatedXMLElement.evaluateXPathExpression("//"+myTag);
              return arrayOfMatches;
          }
      
      
          return [];
      }
      
      
      
        • 1. Re: adding xmlattribute issue
          Jump_Over Level 5

          HI,

           

          Is it possible to find any break result in  "//do more logic here"
          part of code?

           

          Your pure code works on my side with all ocurrences found

           

          rgds

          • 2. Re: adding xmlattribute issue
            bduffy323 Level 1

            There actually isn't any other code in the //do more logic here. It really is just a comment that I was going to fill in later but wasn't getting the correct result of all xml elements retaining their attributes so I haven't proceeded on.

             

            I know I find all of the similiarly tagged XmlElements howerver after the script is finished running only the last one I encountered shows the attribute in the scructure panel. When you run it does every matching xmlelement show the uniqueGroupName attribute?

            • 3. Re: adding xmlattribute issue
              Jump_Over Level 5

              Hi,

               

              I created a doc with few text boxes and other pageItems.

              Two of them have taagged text's parts (4 different kinds; target one with few occurences)

              So structure is :

              Root

                   Story

                        1_tag

                        2_tag

                • Test = content              

                        3_tag

                        2_tag

                • Test = content    

                        4_tah

                   Story

                        1_tag

                        2_tag

                • Test = content

                        3_tag

                        2_tag

                • Test = content

                        2_tag

                • Test = content

                        4_tag

               

              no attributes set at start.

              Script apply "Test" attribute with "content" value to each 2_tag of both Story elements.

              _________________

              var i, k, PI, MF, tags;

              PI = app.activeDocument.pages[0].allPageItems;

              for (i=0; i<PI.length;i++) {

                  MF = PI[i].getElements()[0];

                  tags = xml_find(MF);

              for (k = 0; k<tags.length; k++)

                  tags[k].xmlAttributes.add("Test","content");

              }

              function xml_find(item) {

                  var MFname, temp, arrayOfMatches;

                  if (item.constructor.name == "TextFrame")

                  {

                      MFname = MF.name;

                      temp = MF.parentStory.associatedXMLElement;

                      if (temp==null) return [];

                      arrayOfMatches = temp.evaluateXPathExpression("tag_2");

                      return arrayOfMatches;

                  }

              return []

              }

              __________________

               

              I included MFname to set breakpoint there for debug

              rgds

               

              Message was edited by: Jump_Over

              1 person found this helpful
              • 4. Re: adding xmlattribute issue
                bduffy323 Level 1

                I created a new document and pulled this code out of my larger project and you are correct, it works as it is intended too. I don't know why it isn't working within my project though, I will look into deeper. Thanks for you time and verification of the code.