S
S
Shimpanze2020-06-19 06:48:31
XPath
Shimpanze, 2020-06-19 06:48:31

How to find the following-sibling AND the node itself?

Hello!

<root>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li><div><a>6</a></div></li>
<li>7</li>
<li>8</li>
</root>


How in this code to find all the nodes before the node li/div/aAND the node itself from which we start ( li/div/a)?

Should return:

<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li><div><a>6</a></div></li>


Tried like this:

//li[div/a]/self::node()[self::node() and self::node()/preceding-sibling::li]


Doesn't work for some reason. Although it should.

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2020-09-08
@Shimpanze

You have an error in the title of your question. following is after the node. The correct expression would be:
//li[div/a]/(preceding-sibling::li|self::li)
If there is any misunderstanding in this expression, then ask. I can explain at each step of addressing.
There is a desire to understand XPath deeper, then I recommend an introductory video: XPath is strong!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question