Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a node with XPath whose child node contains a specific inner text

Tags:

xml

xpath

Given the following XML:

<root>
    <li><span>abcText1cba</span></li>
    <li><span>abcText2cba</span></li>
</root>

I want to select all li elements having a span child node containing an inner text of Text1 - using an XPath.

I started off with /root/li[span] and then tried to further check with: /root/li[span[contains(text(), 'Text1')]]

However, this does not return any nodes. I fail to see why, can somebody help me out?

like image 273
D.R. Avatar asked Aug 09 '14 17:08

D.R.


People also ask

How do I get the inner text of XPath?

Show(val. ToString()); I would paste the XPath value in TextBox2, which should inspite give my the inner text corresponding to the XPath.

What is child :: In XPath?

As defined in the W3 XPath 1.0 Spec, " child::node() selects all the children of the context node, whatever their node type." This means that any element, text-node, comment-node and processing-instruction node children are selected by this node-test.

How do I select the first child in XPath?

The key part of this XPath is *[1] , which will select the node value of the first child of Department .


2 Answers

Just for readers. The xpath is correct. OP: Perhaps xpath parser didnt support the expression?

/root/li[span[contains(text(), "Text1")]]
like image 188
Daniel Kereama Avatar answered Sep 19 '22 14:09

Daniel Kereama


//li[./span[contains(text(),'Text1')]] - have just one target result
//li[./span[contains(text(),'Text')]] - returns two results as target

This approach is using something that isn't well documented anywhere and just few understands how it's powerful

enter image description here

Element specified by Xpath has a child node defined by another xpath

enter image description here

like image 39
ViliamS Avatar answered Sep 22 '22 14:09

ViliamS