adding xmlattribute issue
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 [];
}