3 Replies Latest reply on Dec 3, 2015 2:04 AM by steverocket2

    compare two xml attribute values and get their parent element

    aviR

      Hello All

       

      I m stuck at a place.

      I am getting the values of two xml element attributes using two separate functions. I want to compare these values and if they are equal i want to insert a textframe in it's first insertion point.

      My code:

      function Flow(){

        var myDoc = app.activeDocument;

        var myRuleSet = new Array (new flow1);

        with(myDoc){

        var elements = xmlElements;

        __processRuleSet(elements.item(0), myRuleSet);

        }

      }

       

       

      function flow1(){

          this.name = "flow1";

          this.xpath = "//xref";

          this.apply = function(myElement,myRuleProcessor)

              {

                     __processChildren(myRuleProcessor);

                     app.select(myElement);

                     xref_att = myElement.xmlAttributes.item("rid").value;

                  

      }

      function flow2(){

            this.name="fig";

            this.xpath="//fig";

            this.apply=function(elm,ruleproc)

            {

                  __processChildren(ruleproc);

                 app.select(elm);

                  figele=elm.xmlattributes.item("id").value;

              }

            }

       

      My requirement is:

      if xref_att =figele, i have to insert textframe at the first insertionpoint of the element.

       

      How can i do that?

       

      Please help.

      Thank You

        • 1. Re: compare two xml attribute values and get their parent element
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Not sure I got it all but here is an attempt:

           

          var main = function(){
            var doc, xes, n, xf = File ( Folder.temp+"/tmp.xml"), 
            xo, nodes, found = [], xrefs, processed = {}, nid;
            if ( !app.documents.length) return;
          
            doc = app.activeDocument;
          
            //Passing through a xml file
            //to instantiate xml object
            //and avoid dealing with indesign xmlElements as much as possible
            doc.exportFile ( ExportFormat.XML, xf );
            xf.open("r");
            xo = XML( xf.read() );
            xf.close();
          
            //looking at fig nodes within the xml
            nodes = xo..fig.(@id!="");
            n = nodes.length();
          
            //if no fig nodes found return
            if ( !n) return;
          
            //otherwise we look at any xref tags with similar value
            while ( n-- ) {
            nid = String(nodes[n].@id);
            if ( !processed[nid] ){
            processed[nid] = true;
            xrefs = xo..xref.(@id==nid);
            xrefs.length() && found[ found.length ] = nid;
            }
            }
          
            //Getting every matches
            n = found.length;
          
            //If compliant nodes found
            //we loop through matches to add frames
            if (n>0){
            while ( n--){
            xes = doc.xmlElements[0].evaluateXPathExpression("//xref[@id='"+found[n]+"']");
            xes.length && addFrame( xes );
            }
            }
          
            //removing unwanted file
            xf.remove();
          }
          
          
          function addFrame(xes){
            var n = xes.length;
            while ( n-- ) {
            var tf = app.activeDocument.textFrames.add();
            tf.anchoredObjectSettings.insertAnchoredObject(xes[n].texts[0].insertionPoints[0]);
            tf.anchoredObjectSettings.anchoredPosition = AnchorPosition.INLINE_POSITION;
            }
          
          
          }
          
          
          main();
          

           

           

          HTH

          Loic

          www.ozalto.com

          • 2. Re: compare two xml attribute values and get their parent element
            aviR Level 1

            I am trying this...

             

             

            function flow1(){

                this.name = "flow1";

                this.xpath = "//xref";

                this.apply = function(myElement,myRuleProcessor)

                    {

                           __processChildren(myRuleProcessor);

                      

                       app.select(myElement);

                       bMatch= false;

                       var xAtt1=myElement.xmlAttributes.item(0);

                       if(xAtt1.value=="fig"){

                            xref_att = myElement.xmlAttributes.item("rid").value;

                         flow2(myElement,xref_att);

                           }

                       

                            }

             

             

                        }

              

             

            function flow2(myElement,xref_att){

                 this.name="fig";

                 this.xpath="//fig";

                 this.apply=function(elm,ruleproc)

                 {

                       __processChildren(ruleproc);

                      app.select(elm);

                       figele=elm.xmlattributes.item("id").value;

                       if(xref_att ==figele){

                             var ins=myElement.insertionPoints[0] ;

                             var tf=ins.textFrames.add();

                           }

                     

                     

                     }

                 }

             

             

             

             

             

            It is getting the respective "xref" tag but then it is stopping the execution. It's not going further into the second function. Where am I wrong?

            Please help

             

            Thank You

            • 3. Re: compare two xml attribute values and get their parent element
              steverocket2 Level 1

              Your ruleSet is using only the first function:

              Try

               

              var myRuleSet = new Array (

               

                        new flow1,

                        new flow 2

               

              );