xpath to get all the childrens text
Asked Answered
H

2

43

Is there any way to get all the childrens node values within the ul tag.

Input:

<ul>
    <li class="type">Industry</li> 

    <li><a href="/store/Browse/?N=355+361+4294855087">Automotive</a></li>                            

    <li><a href="/store/Browse/?N=355+361+4294855065">Parts </a></li>                                

    <li>Tires</li>                  
</ul>

Output: Industry, Automotive, Parts, Tires.

Humidify answered 2/5, 2012 at 12:22 Comment(0)
T
38

This will retrieve all text elements with a parent ul element.

//ul/descendant::*/text()
Transhumance answered 2/5, 2012 at 15:14 Comment(2)
Tried this and it wouldn't work: /a[not(contains(descendant::*/text(),'Networks'))]Forestation
More accurate answer is to use descendant-or-self to get the text of the element itself, if the element does not have any children.Reptant
J
27

You can use XPath axis. An axis represents a relationship to the context node, and is used to locate nodes relative to that node on the tree. As of today there are 13 axes. You can use descendant for all of the children (including nested) of the context node or descendant-or-self axis which indicates the context node and all of its descendants. For example:

//ul/descendant::*/text()
//ul/descendant-or-self::*/text()
Joubert answered 29/10, 2019 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.