2 Replies Latest reply on Nov 19, 2009 11:24 AM by Jeremy bowmangraphics-DQuh1B

    [CS3, JS] XML Rules: Get geoBounds of <img> Element in Same Branch

    tobias.wantzen Level 1

      Hi,

       

      given the following xml structure:

       

      <root>
      <entry>
           <img href=""></img>
           <title></title >
           <subtitle></subtitle>
      </entry>
      <entry>
           <img href=""></img>
           <annotation></annotation>
           <title></title >
           <subtitle></subtitle>
      </entry>
      </root>
      
      

       

      If an annotation tag exists inside an entry, it is transferred into an inlineFrame. The position of this inlineFrame depends on the geometricBounds of the image of this entry. To get the geometricBounds of the img, I tried to climb up the tree (from <annotation> to <entry>) and walk down again (inside the same branch) into the <img>.

       

      Now my question: Is there another (easier) way to get the right xmlElement (<img> inside same <entry> as <annotation>) as with my »for if construction« in the following sample code?

       

      ...
      this.xpath = "/root/entry/annotation";
      this.apply = function(myElement, myRuleProcessor){
      with(myElement){
           var the_anchored_frame = placeIntoInlineFrame([20,7.5]);
           var frameBounds = the_anchored_frame.geometricBounds;
      
           var theParentElements = myElement.parent.xmlElements;
           for (i=0; i < theParentElements.length; i++) {
                if ( theParentElements[i].markupTag.name == "img" ) {
                     var theIMGBounds = theParentElements[i].xmlContent.geometricBounds;
                     break;
                     }
                }
      ...
      
      Thanks
      Tobias

        • 1. Re: [CS3, JS] XML Rules: Get geoBounds of <img> Element in Same Branch
          Dirk Becker Level 4

          I could be wrong with that because I never used that feature, and it is a while since I looked at XML rules. But anyway:

           

          The xml rules processor should support multiple xpaths / rules in one big swoop. You could store some intermediate results when it hits the first, recover and continue on the second hit and eventually clean up in the error case when either of them is missing.

           

          I think there are also ways to navigate up and down on different xpath axes so you could even get along with one expression, but when I tried the last time support of such advanced xpath features was "limited". Especially as you're talking about CS3.

           

          Dirk

          • 2. Re: [CS3, JS] XML Rules: Get geoBounds of <img> Element in Same Branch
            Jeremy bowmangraphics-DQuh1B Level 3

            I'm a beginner with XML Rules and scripting, but for what it's worth I would go about it by defining a "global" variable. (In case I'm using the wrong terminology, I mean a variable for the entire script rather than one that is limited to a particular function).

             

            For example, before any functions are called, you might add:

             

            var myImageBounds = new Array;

            var myRuleSet = new Array (new ProcessEntry,

            new ProcessTitle,

            new ProcessImg,

            new ProcessAnnotation,

            new ProcessSubtitle);

            with(myDocument){

            var elements = xmlElements;

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

            }

             

            -------------------------------------------------------

            ...then inside the <img>-processing function get the required info, possibly something like this (using Olav Kvern's very helpful "myGetBounds" function from the standard sample scripts):

            -------------------------------------------------------

             

            function ProcessImg(){

            this.name = "ProcessImg";

            this.xpath = "/root/entry/img";

            this.apply = function(myElement, myRuleProcessor){

            with(myElement){

            var myBounds = myGetBounds(myDocument, myDocument.pages.item(-1));

            myBounds = [myBounds[0]+100, myBounds[1]+100, myBounds[2], myBounds[3]];

            var myRectangle = placeIntoFrame(myPage, myBounds);

            myRectangle.fit(FitOptions.frameToContent);

            myImageBounds = myRectangle.geometricBounds;

            }

            return true;

            }

            }

             

            -------------------------------------------------------

            ...then inside the <annotation>-processing function use the info you just got:

            -------------------------------------------------------

             

            function ProcessAnnotation(){

            this.name = "ProcessAnnotation";

            this.xpath = "/root/entry/annotation";

            this.apply = function(myElement, myRuleProcessor){

            with(myElement){

            var the_anchored_frame = placeIntoInlineFrame([20,7.5]);

            the_anchored_frame.geometricBounds = myImageBounds;

            }

            return true;

            }

            }

             

            That's how I would avoid climbing up and down trees, anyway. But I'm a beginner really, so I may have misunderstood your question.

             

            Jeremy