Copy link to clipboard
Copied
Hi,
I need to get specific element in indesign document using xpath. But its returning null dono why... Root element contains namespace. Whats the issue in this?? I have used the below code. Need to get section elements.

var doc = app.activeDocument;
var myRoot = doc.xmlElements.item(0);
alert("myRoot "+myRoot.markupTag.name)
var elts = myRoot.evaluateXPathExpression("*"); //
alert("elts:" +elts + "\nLength:"+elts.length) //
Thanks in advance,
Sudha K
Copy link to clipboard
Copied
Hi Sudha,
Try this ...
var doc = app.activeDocument;
var myRoot = doc.xmlElements.item(0);
alert("myRoot "+myRoot.markupTag.name)
var elts = myRoot.evaluateXPathExpression("descendant-or-self::*")
alert("elts:" +elts + "\nLength:"+elts.length)
Copy link to clipboard
Copied
Hi,
Its return elements length is 0. Did not return elements.
Copy link to clipboard
Copied
I think evaluateXPathExpression does not place you into the parent element as a current point of your xPath expression.
Could you modify and try this :
var doc = app.activeDocument;
var myRoot = doc.xmlElements.item(0);
alert("myRoot "+myRoot.markupTag.name)
var elts = myRoot.evaluateXPathExpression("/")
alert("elts:" +elts + "\nLength:"+elts.length)
Copy link to clipboard
Copied
Hi,
Sorry for late reply....
Its also return 0.
Copy link to clipboard
Copied
Hi,
You must declare the xml namespace ...
var doc = app.activeDocument;
var myRoot = doc.xmlElements.item(0);
if( !myRoot.xmlAttributes.itemByName("xmlns:xml").isValid ){
myRoot.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" );
}
alert("myRoot "+myRoot.markupTag.name);
var elts = myRoot.evaluateXPathExpression("descendant-or-self::*");
alert("elts:" +elts + "\nLength:"+elts.length);
Regards
Copy link to clipboard
Copied
Dear Sudha,
var doc = app.activeDocument;
var myRoot = doc.xmlElements.item(0);
alert("myRoot "+myRoot.markupTag.name)
var elts = myRoot.evaluateXPathExpression("//body/section")
alert("elts:" +elts + "\nLength:"+elts.length)
Copy link to clipboard
Copied
Hi,
Sorry... Its also returning 0... I dono the problem.
Copy link to clipboard
Copied
You have declare xml namespace ?
- if( !myRoot.xmlAttributes.itemByName("xmlns:xml").isValid ){
- myRoot.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" );
- }
Copy link to clipboard
Copied
Place before myRoot element.
app.activeDocument.xmlElements.item(0).xmlAttributes.item("xml:lang").remove();
app.activeDocument.xmlElements.item(0).xmlAttributes.item("xmlns:xsi").remove();
Copy link to clipboard
Copied
Not a good idea to remove namespace and attribute ... It's preferable to add missing namespace.
Copy link to clipboard
Copied
Use xml-rules instead of evaluateXPathExpression: they work well with namespaces (at least for me).
Copy link to clipboard
Copied
Ok thank you...
Copy link to clipboard
Copied
Since your root seems to have only one child, the evaluateXpathExpression will return a XML with that child element, and not an XMLList.
Try:
alert("elts:" +elts + "\nLType:"+elts.nodeKind()) //
Also, i just noticed you are using "length" wrong. For xml it is a function call:
alert("elts:" +elts + "\nLength:"+elts.length())
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more