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

Get elements using xpath

Contributor ,
Sep 09, 2015 Sep 09, 2015

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.

Screen shot 2015-09-09 at 3.02.18 PM.png

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

TOPICS
Scripting
1.6K
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
Enthusiast ,
Sep 09, 2015 Sep 09, 2015

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)

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
Contributor ,
Sep 09, 2015 Sep 09, 2015

Hi,

     Its return elements length is 0.  Did not return elements.

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 ,
Sep 10, 2015 Sep 10, 2015

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)

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
Contributor ,
Sep 21, 2015 Sep 21, 2015

Hi,

   Sorry for late reply....

  Its also return 0. 

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
Enthusiast ,
Sep 21, 2015 Sep 21, 2015

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

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
New Here ,
Sep 21, 2015 Sep 21, 2015

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)

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
Contributor ,
Sep 21, 2015 Sep 21, 2015

Hi,

     Sorry... Its also returning 0...  I dono the problem.

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
Enthusiast ,
Sep 21, 2015 Sep 21, 2015

You have declare xml namespace ?

  1. if( !myRoot.xmlAttributes.itemByName("xmlns:xml").isValid ){ 
  2.     myRoot.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" ); 
  3. }
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
New Here ,
Sep 22, 2015 Sep 22, 2015

Place before myRoot element.

app.activeDocument.xmlElements.item(0).xmlAttributes.item("xml:lang").remove();

app.activeDocument.xmlElements.item(0).xmlAttributes.item("xmlns:xsi").remove();

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
Enthusiast ,
Sep 22, 2015 Sep 22, 2015

Not a good idea to remove namespace and attribute ... It's preferable to add missing namespace.

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
Valorous Hero ,
Sep 22, 2015 Sep 22, 2015

Use xml-rules instead of evaluateXPathExpression: they work well with namespaces (at least for me).

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
Contributor ,
Sep 23, 2015 Sep 23, 2015

Ok thank you...

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
Advisor ,
Sep 24, 2015 Sep 24, 2015
LATEST

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())

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