2 Replies Latest reply on Apr 12, 2011 11:47 PM by John Hawkinson

    Get taged text

    xmlapi Level 1

      In each textframe on each page, I have a textframe called mainText. In the maintext story editor, I have many tags. One of the tags is OfficeNo.

       

      How do I use a script to extract the text out of this tag?

        • 1. Re: Get taged text
          John Hawkinson Level 5

          Traverse the XML tree associated with your textframe looking for all elements that contain the tag.

           

          Untested example:

           

          function traverse(t, p) {
              var r = [], i;
              if (p(t)) {
                  r = r.concat(t);
              }
              for (i=0; i<t.xmlElements.length; i++) {
                  r = r.concat (traverse(t.xmlElements[i], p));
              }
              return r;
          }

          var nodelist, i;

          nodelist = traverse(maintext.associatedXMLElement,
                     function(n) { return n.markupTag.name=="OfficeNo"; });
          for (i=0; i<nodelist.length; i++) {
               alert("Text is "+nodelist[i].contents);
          }

           

          Alternatively, export the document's XML tree and then use a real XML tool on it, like an XSLT processor or an XPath evaluator.

          • 2. Re: Get taged text
            John Hawkinson Level 5

            Whoops, sorry, that's wrong. Or at least, much too general.

            In this case you can use InDesign's limited XPath facilities (they have problems in some cases, including the last time I had to do this, but not for something this straightforward):

             

            var nodelist, i;
            nodelist = maintext.associatedXMLElement
              .evaluteXPathExpression("//OfficeNo");
            for (i=0; i<nodelist.length; i++) {
                 alert("Text is "+nodelist[i].contents);
            }