Reputation: 2411
I'm trying to retrieve up a category hierarchy from the following markup:
<ul class="categories">
<li>My top level category
<ul>
<li>my second level category</li>
</ul>
</li>
</ul>
I want to get the name of the top level category using the following xpath:
//ul[@class='categories']/li/text()
But that obviously returns all of the text of that li including the second level categories.
How do I retrieve on the top level category using an xpath. You can assume that categories will only be two levels deep (Top level and second level categories).
Upvotes: 2
Views: 809
Reputation: 111786
But that obviously returns all of the text of that li including the second level categories.
Actually, no, //ul[@class='categories']/li/text()
will only return the immediate children text nodes of the matching //ul[@class='categories']/li
elements. In this case: My top level category
.
Had you specified either of the following XPaths, you'd have also selected my second level category
:
//ul[@class='categories']/li//text()
string(//ul[@class='categories']/li)
Upvotes: 1