4

I want to get a nested DIV tag using KRL query() but it complains with

ERROR Rules.pm a8x40 show_xfers Ruleset a8x40 failed: html.query error - Invalid specification ">div" in query: div.recent-transfer>div 

Here's the HTML fragment (there are multiple in the file):

<div class='recent-transfer'>
    <span>...</span>
    <div> <!-- * * * -->
        <div>...</div>
        <div>...</div>
    </div>
</div>

Here's my function:

recent = function() {
    t = http:get(the_url).pick("$..content");
    t.query("div.recent-transfer>div")
}

I want to select the DIV marked * * *. Do I need to chain several query() statements to get the DIV?

Randall Bohn
  • 2,597
  • 2
  • 16
  • 20
  • 4
    Original libraries did not support the '>' (child) operator. Recently rolled out new servers that had an updated library that does (Should also support '+' btw). Will have to push updated libraries to all servers--probably later today, maybe tomorrow morning. – MEH Mar 29 '11 at 19:37
  • `@MEH` Your comment would be better suited as an answer, as it is, after all, an answer. – Alex Mar 30 '11 at 03:33

2 Answers2

3

When I tried to reproduce your problem, I didn't get the same error. Instead, I would get a "NOT_FOUND_ERR: DOM Exception 8". In my case, it wasn't a problem with the selector at all; it was the fact that the return value of t.query was an array. If I wanted to use that in, say, a notify(), I had to get the 0th element out of the array and return that instead.

I don't know if that is the same problem you are having. But here's a sample ruleset that works for me:

ruleset a163x61 {
  meta {
    name "Selector test"
    description <<
        Testing the query() function
    >>
    author "Steve Nay"
    logging on
  }

  dispatch {
  }

  global {
    the_url = "http://students.cs.byu.edu/~snay2/content.html";

    recent = function() {
        t = http:get(the_url).pick("$..content");
        // This produces an array.
        text = t.query("div.recent-transfer>div");
        // We want the text out of the element. Get the first element.
        text[0];
        // This won't work; throws a "NOT_FOUND_ERR: DOM Exception 8"
        //text;
    };   
  }

  rule first_rule {
    select when pageview ".*" setting ()
    pre {
        disp = recent();
    }
    notify("Content:", disp) with sticky=true;
  }
}
Steve Nay
  • 2,819
  • 1
  • 30
  • 41
  • // 2011/03/28 07:21:09 ERROR Rules.pm a421x42 xfers Ruleset a421x42 failed: html.query error - Invalid specification ">div" in query: div.recent-transfer>div – Randall Bohn Mar 28 '11 at 13:22
  • 1
    Seems to work about half the time. Try http://www.waahui.com/kalculator, refreshing the page will re-run the app. – Randall Bohn Mar 28 '11 at 13:24
  • +1 for using waahui :) And sure enough, it only works about 1/3 of the time. – Mike Farmer Mar 29 '11 at 17:37
3

"div.recent-transfer>div" is a valid query. There was a problem in the KNS causing intermittent failures.

Here's how the function is used, such that the returned array doesn't make problems:

rule add_content {
    select when pageview ".*"
    foreach recent() setting (item) {
        append("div#main", item);
    }
}
Randall Bohn
  • 2,597
  • 2
  • 16
  • 20