5 Replies Latest reply on Jul 16, 2013 2:32 AM by Sajeev Sridharan

    Getting first occurrence of XML elements

    Sajeev Sridharan Level 4

      Hi All,

       

      My XML tag looks like this:

       

      <xref rid="id001" ref-type="Test">

      <xref rid="id002" ref-type="Test">

      <xref rid="id003" ref-type="Test">

      <xref rid="id004" ref-type="Test">

       

      Code:

       

      var root = app.activeDocument.xmlElements[0];

      var xrefRid = root.evaluateXPathExpression('//xref[@rid]');

      var xrefType = root.evaluateXPathExpression('//xref[@ref-type]');

       

      for(i=0; i<xrefRid.length; i++)

      {

          var refTypeValue =xrefRid[j].xmlAttributes.itemByName("ref-type").value;

          var ridValue =xrefType[j].xmlAttributes.itemByName("rid").value;

       

           if(refTypeValue == "Test")

           {

                alert(ridValue);

           }

      }

       

      In my document multiple number of same "rid" is there, but I want the first occurence of the xref tag.

       

      For example, if <xref rid="id001" ref-type="Test"> will occur on different pages in a book, but i need the first occurence of the elements.

       

      Hope someone can help me...

       

       

      Vandy

        • 1. Re: Getting first occurrence of XML elements
          Fred Goldman Level 3

          If they are all in the same story I sort them by insertionPoint index using the Array.sort() method. I wonder if someone here has a better way.

          • 2. Re: Getting first occurrence of XML elements
            drerol74 Level 2

            Hello Vandy,

             

            I do not know how your XML structure is built up, but perhaps in this way:

             

            var _firstXref = _root.evaluateXPathExpression("//Story[position()=1]/xref[@rid][position()=1]");

             

             

            Roland

            • 3. Re: Getting first occurrence of XML elements
              Sajeev Sridharan Level 4

              Thanks for your reply roland.

               

              But it is not working fine. My XML structure will looks like this,

               

              <DOCUMENT>

              <CN>1</CN>

              <CT>Title</CT>

              <p1>Sample Text Sample Text Sample Text Sample Text Sample Text Sample Text</p1>

              <p>Sample Text Sample Text Sample Text <xref rid="id001" ref-type="Test"> Sample Text Sample Text Sample Text</p>

              <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"> Sample Text Sample Text Sample Text Sample Text</p>

              <p>Sample Text Sample Text <xref rid="id001" ref-type="Test"> Sample Text Sample Text Sample Text Sample Text</p>

              <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"> Sample Text Sample Text Sample Text Sample Text</p>

              </DOCUMENT>

               

              I want the first occurence of the xref tag, which I have highlighted in bold face.

               

               

              Vandy

              • 4. Re: Getting first occurrence of XML elements
                drerol74 Level 2

                If your structure is built in the same way as above, this XPath works fine:

                 

                var _root = app.activeDocument.xmlElements.firstItem();

                var _firstXrefID001andID002 = _root.evaluateXPathExpression("//p[position()=1]/xref[@rid='id001']|//p[position()=2]/xre f[@rid='id002']");

                app.select(_firstXrefID001andID002);

                 

                 

                If other positions of <p> are possible, e.g.

                 

                <DOCUMENT>

                      <CN>1</CN>

                      <CT>Title</CT>

                      <p1>Sample Text Sample Text Sample Text Sample Text Sample Text Sample Text</p1>

                      <p>Sample Text Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id001" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                      <p>Sample Text Sample Text <xref rid="id002" ref-type="Test"/> Sample Text Sample Text Sample Text Sample Text</p>

                </DOCUMENT>

                 

                You can try these XPathsExpressions and take the first element of resulting Arrays:

                 

                var _root = app.activeDocument.xmlElements.firstItem();

                var _firstXrefID001 = _root.evaluateXPathExpression("//p/xref[@rid='id001']");

                app.select(_firstXrefID001[0]);

                 

                 

                var _firstXrefID002 = _root.evaluateXPathExpression("//p/xref[@rid='id002']");

                app.select(_firstXrefID002[0]);

                 

                 

                Roland

                • 5. Re: Getting first occurrence of XML elements
                  Sajeev Sridharan Level 4

                  Thanks Roland for your speedy reply.

                   

                  Really helps a lot.

                   

                  Vandy