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

Getting XML child elements

Participant ,
Mar 18, 2015 Mar 18, 2015

Dear Indesign scripters,

This script is making from all the XML tags ParagraphStyles.

But it refuses to take the child elements. Do I have to use XPath expression? And how?

function main() { 

  var xes, n, doc, ps, xe; 

  if ( !app.documents.length ) return; 

  doc = app.activeDocument;

  xes = doc.xmlElements[0].xmlElements; 

  n = xes.length; 

  while ( n-- ) { 

  xe = xes;

  st = doc.paragraphStyles.itemByName (xe.markupTag.name ); 

  !st.isValid && doc.paragraphStyles.add({name:xe.markupTag.name}); 

  } 

 

 

main();

The XML

<Workbook> 

     <Element_A_01> 

          <Element_A_01></Element_A_01> 

     </Element_A_01> 

     <Element_B_02> 

          <Element_B_02></Element_B_02> 

     </Element_B_02> 

</Workbook>

Greetings from Holland

TOPICS
Scripting
1.0K
Translate
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 , Mar 18, 2015 Mar 18, 2015

The code creates two paragraph styles namely Element_A_01 and Element_B_02. These two are created for the immediate child nodes of the Workbook node. Now it seems that you are trying to create pstyles for all the nodes in the XML, which in your case would still be these two as Element_A_01 has a child node that is also named Element_A_01 and the same is the case for Element_B_02.

If i get your point right then you need to create 4 pstyles if all the nodes have different names, but that is not wor

...
Translate
Community Expert ,
Mar 18, 2015 Mar 18, 2015

The code creates two paragraph styles namely Element_A_01 and Element_B_02. These two are created for the immediate child nodes of the Workbook node. Now it seems that you are trying to create pstyles for all the nodes in the XML, which in your case would still be these two as Element_A_01 has a child node that is also named Element_A_01 and the same is the case for Element_B_02.

If i get your point right then you need to create 4 pstyles if all the nodes have different names, but that is not working. For this you will have to recursively traverse each node that you get from the code xes.

The code be something like this

  1. function main() {   
  2.   var xes, n, doc, ps, xe;   
  3.   if ( !app.documents.length ) return;   
  4.   doc = app.activeDocument; 
  5.   xes = doc.xmlElements[0].xmlElements; //This gives the immediate child nodes of the Workbook node, hence the length in the next statement is 2  
  6.   n = xes.length;   
  7.   while ( n-- ) {   
  8.   xe = xes;   //Here you need to recursively traverse the node xe
  9.   //The no of child elements of xe can be obtained as xe.xmlElements.length
  10.   //First element can be obtained by xe.xmlElements[0]
  11.   }   
  12. }   
  13.    
  14.    
  15. main();

Hope this solves the issue

Translate
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
Participant ,
Mar 19, 2015 Mar 19, 2015
LATEST

Thanks for the help,

It's a bit of an struggle for me (noob) but i'm getting there.

Now I've got it with this script, but is this the right way?

function main() { 

  var xes, n, doc, ps, xe; 

  if ( !app.documents.length ) return; 

  doc = app.activeDocument;

  xes = doc.xmlElements[0].xmlElements;

  n = xes.length; 

  while ( n-- ) { 

  xe = xes;

    try

    {

  st = doc.paragraphStyles.itemByName (xe.markupTag.name );

  !st.isValid && doc.paragraphStyles.add({name:xe.markupTag.name});

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

  st = doc.paragraphStyles.itemByName (xe.xmlElements[0].markupTag.name );

  !st.isValid && doc.paragraphStyles.add({name:xe.xmlElements[0].markupTag.name});

     }

    catch(e){}

  }

}

main();

Greetings for Holland

Translate
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