Skip to main content
Participating Frequently
June 19, 2014
Question

evaluateXPathExpression bug

  • June 19, 2014
  • 1 reply
  • 436 views

i'm trying to generate bookmark of first level element, to achieve that, i'm using evaluateXPathExpression, works perfectly for some files, for some other files.. it is fetching wrong order.., meant fetching based on the xmlElement(id) i hope, instead of XML position. My script is below.

var aDoc=app.activeDocument;

var sectionFirst="//body/*[not(self::titleGroup)]/*[starts-with(local-name(), 'title')]";

//namespace required to execute evaluateXPathExpression

  var rootElement = aDoc.xmlElements[0];

  if( !rootElement.xmlAttributes.itemByName("xmlns:xml").isValid )

  rootElement.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" );

  if(rootElement.xmlAttributes.item("xmlns").isValid )

  rootElement.xmlAttributes.item("xmlns").remove();

var sectionFirstElements=rootElement.evaluateXPathExpression(sectionFirst);

for(var ele=0; ele < sectionFirstElements.length; ele++)

{

  alert(sectionFirstElements[ele].id+" "+sectionFirstElements[ele].contents);

}

InDesign sample file is attached to verify (Dropbox - sample.indd). Does anyone across this issue. please enlighten me on this.

This topic has been closed for replies.

1 reply

Inspiring
June 20, 2014

I wouldn't call this a bug. It is a little surprising, but I don't think xpath necessarily guarantees that query results will come back in document order.

If you go to idml and back, InDesign will rebuild those ids in document order.

Since your results are all in one story, you can of course sort your results by their position in the story:

sectionFirstElements.sort(function (a, b) {

    return a.insertionPoints[0].index - b.insertionPoints[0].index;  

});

Jeff