• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Getting XML attribute value

New Here ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

Hi,

I tried to get particular attribute  value of a element in xml.I used

var famid=mySel.associatedXMLElement.parent.xmlAttributes.itemByName("family_id").value;  

This worked for me for some elements.But for some elements it returned 'Object is Invalid" error.What should be done to get the attribute value.?Please do guide on this..

Regards,

Revathi V

TOPICS
Scripting

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 08, 2018 May 08, 2018

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 des

...

Votes

Translate

Translate
Community Expert ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

HI,

Can you post/share a document that shows the problem?

Regards

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

Hi Malcolm,

Thanks for your response.I tried to attach a document..but i couldn't..How can I attach a document here?

Regards,

Revathi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Hi Revathi,

attaching documents is not possible / allowed.

Upload your files to a service like Dropbox and post the download link here.

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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&sour...

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

LATEST

Hi Malcolm,

I got it.Its working fine.Now I am able to get the ids.Thank you so much for your guidance..

Regards,

Revathi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines