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

Community Beginner ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

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/87620...

 

Thanks,

Yogita

TOPICS
How to , Scripting , SDK

Views

647

Likes

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

Advocate , May 10, 2021 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...

Likes

Translate

Translate
Advocate ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Try this below snippet :

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

generate Xpath.png

Best

Sunil

Likes

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
Advocate ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

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

Likes

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 Beginner ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Thank you so much! It is woking for me.

Likes

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
Participant ,
Feb 21, 2022 Feb 21, 2022

Copy link to clipboard

Copied

LATEST

In that script, having elements with the same name in the same hierarchy might not work.

What about this script?

if(app.activeDocument.selection[0].constructor.name == "XMLElement"){

    absoluteXPath = getAbsoluteXPath(app.activeDocument.selection[0],"");
    
    alert(absoluteXPath);
}

function getAbsoluteXPath(targetElement,absoluteXPath){

    var elementNameCount = 0;
    
    if(targetElement.parent.constructor.name == "XMLElement"){

        for(var i = 0; i <=  targetElement.index; i++){
                
            if(targetElement.parent.xmlElements.item(i).markupTag.name == targetElement.markupTag.name){
                    
                elementNameCount++;
            }
        }
    
        absoluteXPath = "/" + targetElement.markupTag.name + "[" + elementNameCount + "]" + absoluteXPath ;
                        
        absoluteXPath = getAbsoluteXPath(targetElement.parent,absoluteXPath);
    
    }else{
        
        absoluteXPath = "/" + targetElement.markupTag.name  + absoluteXPath ;
    }

    return absoluteXPath;
}

Likes

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