greatwitenorth greatwitenorth - 3 months ago 9
HTML Question

XPath of immediate child text vs all descendant text?

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).

Answer

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)