12 Replies Latest reply on Dec 22, 2011 7:00 AM by absqua

    InDesign XML traversal Question?

    கற்பனை (Imagine) Level 3

      Hi All,

       

      XML.jpg

       

      I've the three types of the XML structure in Input XML,   the above snapshot for your easy references. The problem how to identify the three pattern of XML in InDesign CS5.5 using Javascript.

       

      Sample 1 has the <p> tag with some para contents after that the list tag was started.  Script eg:  myElement.xmlElements[0].markupTag.name will gives the name of the first xmlelement inside the <p>,  but how to differentiate if the para has immediate list element or few para text is there after that the list comes ?

       

      Is there any possibilities to identify?

       

      Thanks in advance.

      Imagine

        • 1. Re: InDesign XML traversal Question?
          absqua Level 4

          InDesign's limited subset of xpath 1.0 for .evaluateXPathExpression() makes it impossible, I think, to do this that way.

           

          var doc = app.activeDocument;
          
          var root = doc.xmlElements[0];
          
          var elements = root.evaluateXPathExpression("//p[node()[position() = 1 and not(self::*)]]");
          

           

          That should get you your first example—all <p>s whose first child node is not an element node. It seems like it works.

           

          var elements = root.evaluateXPathExpression("//p[List[not(following-sibling::node())]]");
          

           

          That should get you <p>s with <List> children without following siblings, which should match your third example only, but matches all three.

           

          My xpath is rusty, though. Maybe someone else has a better idea. I suppose you could do it by comparing the index of each child element's .xmlContent.

           

          I think if you applied a transform as you imported the xml (where InDesign offers a fuller xpath processor) and marked these different scenarios with an attribute it would work.

           

          Jeff

          • 2. Re: InDesign XML traversal Question?
            John Hawkinson Level 5

            Jeff, my read of Imagine's question was not that (s)he wants to identify the distiction between these two cases by enumerating them, but rather he wants to traverse the tree, doing something with lists that begin the <p>, something different when text begins the <p>, etc.

             

            Imagine, can you tell us more about your actual intent here?

             

            I would tend to suggest the thing to do is to start in the Story and walk it character by character, looking for the XML. Another, perhaps better, approach, is to export the document to XML, and then use E4X (or something else) to look for text nodes.

             

            But I think really it all depends on what is desired, and that's not clear from the question.

            1 person found this helpful
            • 3. Re: InDesign XML traversal Question?
              கற்பனை (Imagine) Level 3

              I would like to thank both of you reply for my question.  I'm doing the InDesign first XML workflow using genralized DTD docbook.  that xml file has structure like the snapshot.  if using the evaluatateXPathExpression method for <p> tag traversal then apply paragraph style to that paras.

               

              var myParas = root.evaluateXPathExpression("//p");

              var len = myParas.length;

               

              while (len--) {

              // do stuff for apply the paragraph style.

              }

              Its apply all the paras to specific style.  but i  need to identify the different paras and apply to different styles to the para contents only.

               

              In Snapshot the 3rd samples has the <p> elements without any para contents so i want to skip to applying the style for that instances.

               

              Hope this will clear to all.

              • 4. Re: InDesign XML traversal Question?
                John Hawkinson Level 5

                I guess I am unclear about the overall structure here...you are traversing over app.activeDocument.xmlElements and applying different styles to different kinds of elements through a script? Is it a loop? Is it an XML Rule? Is it something else entirely?

                 

                If it's from docbook, then can you just XSLT translate this prior to importing it into XML, as Jeff suggets?

                • 5. Re: InDesign XML traversal Question?
                  கற்பனை (Imagine) Level 3

                  I would like to know for clarifications before going to the XSLT approach.  is InDesign CS5.5 Import XML options supports the XSLT full features or limited version support?  else to doing the XSLT transformation outside the InDesign?

                  • 6. Re: InDesign XML traversal Question?
                    John Hawkinson Level 5

                    I'm not entirely sure of what your grammar means, and whether you concerned with running XSLT outside of InDesign.

                    I don't think it is precisely clear what InDesign's XSLT engine is, but I think we can assume it is XSLT 1.0, and that should be plenty for your application, because XSLT 2.0 adds features that are just not needed here.

                     

                    But if it turns out it doesn't meet your needs, processing it with an external processor should not be a big deal. After all, the docbook process already runs a bunch of tools and processing to produce the documents, so what is one more? (e.g. xsltproc)

                    • 7. Re: InDesign XML traversal Question?
                      கற்பனை (Imagine) Level 3

                      John Hawkinson wrote:

                       

                      I'm not entirely sure of what your grammar means,

                      English is not my first language, and i'm trying to improve language skills.  Thanks to all.

                      • 8. Re: InDesign XML traversal Question?
                        John Hawkinson Level 5

                        That's fine. We don't see a lot of English babies named கற்பனை, I'll tell you that.

                        But I'm still not sure of your question -- can you clarify, or is it answered?

                        1 person found this helpful
                        • 9. Re: InDesign XML traversal Question?
                          கற்பனை (Imagine) Level 3

                          Jeff answer is almost near to my question. try to go with XSLT approach. Thanks John.

                          • 10. Re: InDesign XML traversal Question?
                            absqua Level 4

                            It's been awhile since I wrote xslt for InDesign to apply as it imports xml, but I remember it behaving in ways that surprised me—and not in a good way—after working mostly with the Saxon processor. It supports full xpath 1.0, but not perfectly, if I remember. I'm sorry I can't provide specific examples of the problems. I know I was able to work around them.

                            • 11. Re: InDesign XML traversal Question?
                              John Hawkinson Level 5

                              Well, Saxon is XSLT 2.0, so it's not surprising there are differences.

                              • 12. Re: InDesign XML traversal Question?
                                absqua Level 4

                                No, I saw differences between InDesign and Saxon 6, which was 1.0. My workflow was to write the transforms in Oxygen, where you could run it using Saxon 6 for 1.0 or whatever the latest version was for 2.0, and then test it in InDesign after I had it working there. I was sometimes disappointed by the results in InDesign, but can't provide specifics, so should probably leave it alone.