Skip to main content
Participating Frequently
May 10, 2021
Answered

Get selected xml element and xpath of the selected node in structure pane by javascript

  • May 10, 2021
  • 1 reply
  • 2262 views

Hi All, 

Is it possible to get selected  XML elements and Xpath of the the selected XML node from the InDesign structure pane?

 

I found below thread which select the XML node in the structure pane but I'm trying to get the selected elements and Xpath of the element.

Reference link: https://community.adobe.com/t5/indesign/select-xml-element-in-structure-pane-by-javascript/m-p/8762062

 

Thanks,

Yogita

Correct answer Sunil Yadav

This below is snippet for it:

 

if(app.selection[0].constructor.name == "XMLElement" || app.selection[0].constructor.name == "XMLAttribute"){
    if(app.selection[0].constructor.name == "XMLAttribute") var node = app.selection[0].parent;
    else var node = app.selection[0];
    var root = app.documents[0].xmlElements[0];
    var tN = node;
    var xPath = "";
    while(tN != root){
        var attr = "/"+tN.markupTag.name;
        for(var x = 0; x < tN.xmlAttributes.length; x++){
            if(tN.xmlAttributes[x].name.indexOf(":") == -1) attr += "[@"+tN.xmlAttributes[x].name+"='"+tN.xmlAttributes[x].value+"']";
            }
        tN = tN.parent;
        xPath = attr+xPath;
        }
    xPath = "//"+tN.markupTag.name+"/"+xPath;
    var dialog = new Window("dialog"); 
    dialog.text = "Generated Xpath"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["right","top"]; 
    dialog.spacing = 10; 
    dialog.graphics.backgroundColor = dialog.graphics.newBrush (dialog.graphics.BrushType.SOLID_COLOR, [0, 0.251, 0.251]);
    var edittext1 = dialog.add('edittext {properties: {name: "edittext1", multiline: true, scrollable: true}}'); 
    edittext1.preferredSize.width = 400; 
    edittext1.preferredSize.height = 100; 
    edittext1.text = xPath;
    var statictext1 = dialog.add("statictext", undefined, undefined, {name: "statictext1"}); 
    statictext1.text = "Created By Sunil Yadav (Adobe Community Professional)";
    edittext1.active = true;
    dialog.show();
    }

 

Best

Sunil

1 reply

Sunil Yadav
Legend
May 10, 2021

Try this below snippet :

First select any XML Element or XML Attribute for XPATH generation & then execute the script.

Best

Sunil

Sunil Yadav
Sunil YadavCorrect answer
Legend
May 10, 2021

This below is snippet for it:

 

if(app.selection[0].constructor.name == "XMLElement" || app.selection[0].constructor.name == "XMLAttribute"){
    if(app.selection[0].constructor.name == "XMLAttribute") var node = app.selection[0].parent;
    else var node = app.selection[0];
    var root = app.documents[0].xmlElements[0];
    var tN = node;
    var xPath = "";
    while(tN != root){
        var attr = "/"+tN.markupTag.name;
        for(var x = 0; x < tN.xmlAttributes.length; x++){
            if(tN.xmlAttributes[x].name.indexOf(":") == -1) attr += "[@"+tN.xmlAttributes[x].name+"='"+tN.xmlAttributes[x].value+"']";
            }
        tN = tN.parent;
        xPath = attr+xPath;
        }
    xPath = "//"+tN.markupTag.name+"/"+xPath;
    var dialog = new Window("dialog"); 
    dialog.text = "Generated Xpath"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["right","top"]; 
    dialog.spacing = 10; 
    dialog.graphics.backgroundColor = dialog.graphics.newBrush (dialog.graphics.BrushType.SOLID_COLOR, [0, 0.251, 0.251]);
    var edittext1 = dialog.add('edittext {properties: {name: "edittext1", multiline: true, scrollable: true}}'); 
    edittext1.preferredSize.width = 400; 
    edittext1.preferredSize.height = 100; 
    edittext1.text = xPath;
    var statictext1 = dialog.add("statictext", undefined, undefined, {name: "statictext1"}); 
    statictext1.text = "Created By Sunil Yadav (Adobe Community Professional)";
    edittext1.active = true;
    dialog.show();
    }

 

Best

Sunil

Participating Frequently
May 11, 2021

Thank you so much! It is woking for me.