'xpath all HTML tags <a> in body except in <footer> limited to x

I need to find all HTML tags a in the body except those from footer and limit them to a certain value.

Without any limitation, I have found this :

//body | //a[not(ancestor::footer)]

Not sure it is correct though.

With the limitation :

//body | //a[not(ancestor::footer)]/a[position()<=25]

But this not work

Or :

//body | //a[not(ancestor::footer)] | /a[position()<=25]

But it does not limit anything

What would be the correct syntax to combine conditions ?



Solution 1:[1]

AFAIK you should not use | there.
Without the amount limitation your XPath could be

//body//a[not(ancestor::footer)]

While with the limitation of output to 25 results only your XPath will be

//body//a[not(ancestor::footer) and position()<=25]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kisa lisa