9 Replies Latest reply on Feb 8, 2017 7:24 AM by Dirk Becker

    Get xmlElements using XPath

    Sudha K Level 1

      Hi,

       

          

           I am getting xml elements using xpath.  I have used the below code to get the xml elements. Its returning null for a document but that document contains the specified xml elements.

       

      var xPath = "//title"

      var doc = app.activeDocument; 

      var myRoot = doc.xmlElements.item(0); 

       

      if( !myRoot.xmlAttributes.itemByName("xmlns:aid").isValid ){ 

          myRoot.xmlAttributes.add("xmlns:aid", "http://ns.adobe.com/AdobeInDesign/4.0/" ); 

       

      alert("myRoot "+myRoot.markupTag.name); 

      var   elts = myRoot.evaluateXPathExpression(xPath); 

      alert( "Length:"+elts.length + "\nelts:" +elts )

       

       

      If i remove the attributes exists (selection), its working.  Is it right way? will it affect anything??

       

      xpatherror.png

       

       

      Thanks in advance,

      Sudha K

        • 1. Re: Get xmlElements using XPath
          Loic.Aigon Adobe Community Professional

          var xPath = "//*:title"

          • 2. Re: Get xmlElements using XPath
            Sudha K Level 1

            Its also returns 0 elements.  If i delete those selected attributes then only i can get elements.  Is it right way??

             

             

            Reading xml elements recursively and xpath is different and having time different?? When document having thousands of elements its taking long time to process.. How Can i reduce the time using script??

            • 3. Re: Get xmlElements using XPath
              Dirk Becker Level 4

              Removing the xmlns attributes is wrong as long the matching namespaces are still used.

              A quick hack and some alternatives were mentioned in Re: How to access XML elements by name in Extendscript??

              As you're talking about longer XML documents, consider an outside XSLT processor to strip down the XML to something better usable.

              ExtendScript XML will give you hard times anyway with large documents - e.g. better don't pass big sub-trees around as function arguments.

              In some cases the InDesign xml import with its prehistoric XSLT processor and different trouble spots could also be an alternative.

              • 4. Re: Get xmlElements using XPath
                Sudha K Level 1

                Sorry... what will the function do??...  what is the parameter xml?? is it indesign xml object?? ie., xml = app.activeDocument.xmlElements.item(0)?

                 

                 

                function removeAllNamespace(xml)

                {

                          var ns =new Namespace();

                          var d=xml.descendants();

                          for (var i=0;i<d.length();i++)

                    d[i].setNamespace(ns);

                }

                 

                 

                I need to get the positions of specified xml element contents in an InDesign document.  For this, how should use indesign only? or there is anyother possiblites??  I completed the process but i could not use this process due to script running time.... Don't know wat to do...

                • 5. Re: Get xmlElements using XPath
                  Dirk Becker Level 4

                  Sorry that I did not make that clear: the other thread is mainly about ExtendScript's E4X XML objects, while you already work with InDesign document based XML. E4X has its own XPath implementation, so if you just need to look up something from any smaller XML that would be an alternative.

                   

                  In your InDesign document based scenario I would use an XSLT to change the XML during import into something that matches your InDesign layout needs. E.g. strip unused elements, remove namespaces from elements and attributes, rearrange them as you need. Eventually import into the document's XML story rather than into text stories (backing text frames).

                   

                  One typical problem with document XML is a missing xmlns:xml namespace attribute, it will hurt XPath searches as soon you use e.g. an xml:lang attribute. In that case have a look at this thread.

                  • 6. Re: Get xmlElements using XPath
                    Sudha K Level 1

                    Thank you....

                     

                    I need to add the below one also with the indesign namespace and then have to use xpath to evaluate evaluateXPathExpression. Is it right??

                     

                     

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

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

                     

                    I have added this but no result for the below code.  If i change the xpath to "//*" its returning count.  For the xpath "//section" its returning 0;

                     

                    var xPth = "//section";

                    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" );

                    }

                     

                    if( !myRoot.xmlAttributes.itemByName("xmlns:aid").isValid ){

                        myRoot.xmlAttributes.add("xmlns:aid", "http://ns.adobe.com/AdobeInDesign/4.0/" ); 

                    }

                     

                    elts = myRoot.evaluateXPathExpression(xPth);

                    alert(elts.length)

                     

                    Screen shot 2017-02-08 at 17.34.49.png

                    • 7. Re: Get xmlElements using XPath
                      Kasyan Servetsky Level 5

                      I had the same problem a while ago. I described my solution --  Prepare xml-attributes script -- here at the bottom of the page. In brief, it temporarily changes : (colons) to _ (uderscore) and, at the end, restores them back to colons.

                      —Kas

                      • 8. Re: Get xmlElements using XPath
                        Sudha K Level 1

                        Thank you....

                         

                        I will check and let you know...

                         

                        One more thing, I have used xpath to process .  It returns 1000's of elements.  will it affect the processing speed??

                         

                        My ques is, if we use recursive element it will take time and it affect the process speed.  When we use xpath it dont affect speed. Can you pls anyone clarify me...

                        • 9. Re: Get xmlElements using XPath
                          Dirk Becker Level 4

                          As adding xmlns:xml did not help, I would look for other namespaces deeper in the document.

                          For Kasyan's approach it will probably be faster to perform the suggested changes via an XSLT executed during import. On the other hand that assumes XSLT knowledge.