Skip to main content
bduffy323
Inspiring
November 15, 2012
Question

adding xmlattribute issue

  • November 15, 2012
  • 1 reply
  • 1296 views

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;

        var xmlElements = findTag(myPageItem,"tagToMatch");

        for (var j = 0; j < xmlElements.length; j++)

        {

            var myXmlElement = xmlElements;

            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 [];

}

This topic has been closed for replies.

1 reply

Jump_Over
Legend
November 16, 2012

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

bduffy323
bduffy323Author
Inspiring
November 16, 2012

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?

Jump_Over
Legend
November 16, 2012

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.getElements()[0];

    tags = xml_find(MF);

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

    tags.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