[JAVA] About the matter that: nth-child (-n + m) does not work as intended in the selector of jsoup

at first

I used jsoup's selector for convenience, but the behavior of: nth-child is strange, so I checked it (too much) I haven't checked it) and the countermeasures. ** I wrote a lot, but I was able to do what I wanted to do with ": lt (n)" instead of ": nth-child (-n + m)". (Description at the end) **

About the environment

Development environment: Eclipse pleiades 4.6.3 Java : Java8 jsoup: 1.9.2 (* 1.10.3 is the same event)

Thing you want to do

I want to get only the first 5 of the multiple elements that I got with selector

When specifying with a normal selector (CSS, JavaScript, etc.)

For example, if you want to get only the a tag in the following elements.

<div class="list">
  <ul>
    <li><a href="aaa">Link 1</a></li>
    <li><a href="aaa">Link 2</a></li>
    <li><a href="aaa">Link 3</a></li>
    <li><a href="aaa">Link 4</a></li>
    <li><a href="aaa">Link 5</a></li>
    <li><a href="aaa">Link 6</a></li>
    <li><a href="aaa">Link 7</a></li>
    <li><a href="aaa">Link 8</a></li>
    <li><a href="aaa">Link 9</a></li>
    <li><a href="aaa">Link 10</a></li>
  </ul>
</div>

The specification is as follows

div.list> ul > li > a

In this case, all 20 cases will be covered. Of these, only the first 5 cases will be written as follows.

div.list> ul > li:nth-child(-n+5) > a

When specifying with the selector of jsoup

If you specify the above, for some reason, "5th and subsequent items" will be the target. In other words, 6 items from "Link 5" to "Link 10" are targeted.

However, by specifying the following, you can target the "first 5 cases".

div.list> ul > li:not(:nth-child(-n+6)) > a

The point is

-Enclose: nth-child with: not --N + 6 instead of -n + 5

is.

I don't know why this works, but it seems to behave like this.

In the first place!

From the comments I received, it was easy to use ": lt ()" ...

div.list> ul > li:not(:nth-child(-n+6)) > a

not

div.list> ul > li:lt(5) > a

You can meet your request with.

I made a noise m (_ _) m

Recommended Posts

About the matter that: nth-child (-n + m) does not work as intended in the selector of jsoup
When the form such as Select of Materialize does not work properly
@BeforeStep does not work in Tasklet of spring-batch
About the matter that the code to read the C structure member (Char array) that was working in swift 2.3 in swift 3 did not work
[Rails] Solving the problem that session timeout does not work
[Rails] About the error that the image is not displayed in the production environment
If the ctrl + shortcut does not work in Eclipse, disabling welcome may help