1 Reply Latest reply on Jun 20, 2014 7:58 AM by absqua

    evaluateXPathExpression bug


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

        • 1. Re: evaluateXPathExpression bug
          absqua Level 4

          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;