Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This was is a tricky one. When there is just one element with class='divclass' the previous answer sure is fine. If there were multiple results from grep, then a find() for a single result is not the answer. Pointing out that the result is an ArrayList is correct. Inserting an outer nested .each() loop provides a GPathResult in the closure parameter <em>div</em>. From here the drill down can continue with the expected result.</p> <pre><code>html."**".grep { it.@class == 'divclass' }.each { div -&gt; div.ol.li.each { linkItem -&gt; def link = linkItem.h3.a.@href def address = linkItem.address.text() println "$link: $address\n" }} </code></pre> <p>The behavior of the original code can use a bit more of an explanation as well. When a property is accessed on a List in Groovy, you'll get a new list (same size) with the property of each element in the list. The list found by grep() has just one entry. Then we get one entry for property <em>ol</em>, which is fine. Next we get the result of ol.it for that entry. It is a list of size() == 1 again, but this time with an entry of size() == 2. We could apply the outer loop there and get the same result, if we wanted to:</p> <pre><code>html."**".grep { it.@class == 'divclass' }.ol.li.each { it.each { linkItem -&gt; def link = linkItem.h3.a.@href def address = linkItem.address println "$link: $address\n" }} </code></pre> <p>On any GPathResult representing multiple nodes, we get the concatenation of all text. That is the original result, first for <em>@href</em>, then for <em>address</em>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload