Hi Uwe,
Thanks for your guidance.I have uploaded the document and the link is
Link: https://www.dropbox.com/s/ht5wb3vxrpe0b7h/Sample1.idml?dl=0https://www.google.com/url?q=https://www.dropbox.com/s/ht5wb3vxrpe0b7h/Sample1.idml?dl%3D0&sa=D&source=hangouts&ust=1525856229236000&usg=AFQjCNHBYFrOvOtnnbg69ciMCu-jdPtuuA
In this document I have placed two xml elements.Please do run the script by selecting the frame.
mySel=app.selection[0];
var famid= mySel.associatedXMLElement.parent.xmlAttributes.itemByName("family_id").value;
alert(famid);
For the first element I am able to get the family id but for the second one I get "Object is invalid"error.How can I get the attribute value here.
Regards,
Revathi
Hi,
I think I have found the problem when running your code this is the result :
Text Frame one ( with the URI looking contents) has 4 attributes - COTYPE, family_id, sub_Families, TBGUID
Whereas
Text Frame two ( with the random text) has only 2 attributes - COTYPE, TBGUID
Therefore the code is failing on the itemByName ("family_id"); part because there is no "family_id" attribute to get.
The reason for this appears to be the structure of your XML in the document.
For text frame one it is a direct descendant of the "product_family" tag, (
whereas
text frame two is a child of a direct descendant of the "product_family" tag.
To fix the code to work for the second text frame you need to add an extra ".parent" so you would have.
mySel=app.selection[0]; var famid= mySel.associatedXMLElement.parent.parent.xmlAttributes.itemByName("family_id").value; alert(famid);
obviously this would then cause the first frame to be incorrect, so you need to somehow detect if you are at the correct level, I would suggest doing something like
mySel=app.selection[0];
var parentNode = mySel.associatedXMLElement.parent
var nodeValue = parentNode.xmlAttributes.itemByName("family_id")
if ( !nodeValue.isValid)
{
nodeValue = parentNode.parent.xmlAttributes.itemByName("family_id");
}
nodeValue = nodeValue.value;
alert ( nodeValue);
Hope this helps
Malcolm