In xpath, how can I find which number (depending on where, 0 or 1 you start, c is 2 or 3), for example, element c has the following list:
<ol> <li>a</li> <li>b</li> <li>c</li> <li>d</li> </ol>
You can use XPath and read previous lisiblings :
li
count(//ol/li[. = 'c']/preceding-sibling::*)
Demo (using lxml.etree):
lxml.etree
>>> from lxml import etree as ET >>> data = """ ... <ol> ... <li>a</li> ... <li>b</li> ... <li>c</li> ... <li>d</li> ... </ol> ... """ >>> tree = ET.fromstring(data) >>> value = "c" >>> int(tree.xpath("count(//ol/li[. = '%s']/preceding-sibling::li)" % value)) 2
Source: https://habr.com/ru/post/1622760/More articles:Скелет WordPress, VVV, Multisite и правильные правила Nginx - phpSet order in debugging variable list in PhpStorm - debuggingVectorKit / OpenGL error in MKMapView with iOS 9 - iosHikariCP: maxLifetime and idleTimeout with fixed-size pool - hikaricpRazor generates jQuery too late for a custom script using it despite Scripts.Render order - javascriptСмущенный jQuery не найден, когда я его знаю - javascriptdplyr - Pivot table for multiple variables - rIOS general permissions: check if user selected "Do not save passwords" in Safari - authenticationЗадержка таймера Java 1 мс слишком быстра? - javaBest way to convert Vector to Matrix in Julia? - arraysAll Articles