简体   繁体   中英

Jsoup select - why does it include current element?

I am trying to understand if I'm missing something, because it seems very bizarre to me why Jsoup includes the current element in the search performed by select .

For example (scala code):

val el = doc.select("div").first
el.select("div").contains(el) // => true

What is the point of this? I see very limited cases where you'd actually want this. Do I need to always use el.children.select instead? Is there a nicer method?

Side question: Is there a nicer way to do el.children.select(s).first ? In Ruby Nokogiri it would be el.at_css(s) which is much shorter, is there a similar option in Jsoup?

As to why the select method was implemented the way it did, my only guess would be because it's the most straightforward way to do it if we take into consideration the struct that holds the data resulted by your query.

If we think about el , we will see that it is a "tree" representation of the elements that you asked for, having as root the first parent div node. Then you call select on that tree. Now it all depends on how you decide to see this tree. Should we treat this "tree" as a whole (include root) or not (discard root)? It's a matter of taste I guess.

If I judge from myself, a lot of people using Jsoup, probably have had some experience on DOM parsing with jQuery. The equivalent would be something like this $("div").first().find("div") where find is documented as

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

This is in agreement with what you stated. It's just a matter of how the two libraries "see" the resulting tree. Jsoup treats the root as one of the nodes, jQuery differentiates the root (as far find is concerned).

About the second part of your question.

val el = doc.select("div").first
el.children.select(s).first

No there isn't. The only way is to change the css selector.

val result = doc.select("div:eq(0) " + s).first;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM