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
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
...
Copy link to clipboard
Copied
Try this below snippet :
First select any XML Element or XML Attribute for XPATH generation & then execute the script.
Best
Sunil
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
Copy link to clipboard
Copied
Thank you so much! It is woking for me.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
With this approach of generating XPath will only find that specific node, even if other elements are available with the same xpath.
So if you don't want to find other matching element with the same xpath then this approach can be used. Some people may say this as hardcoding approach.
And if counter of element changes within the same parent, then this XPath will not work.
Sunil