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

xpath contains(text)
xpath child element
xpath first element
xpath select attribute
xpath innertext
xpath position
xpath text equals
xpath attribute value

Given the following XML:


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?

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

/root/li[span[contains(text(), "Text1")]]

xpath, To find an element containing specific text, you can use the contains function. If you want to find text that spans multiple children/text nodes, then you can use  Comparing comboBox.Slectedvalue with XML file node value using Xpath in C# 1 Converting 45 Fortify .fpr files to .csv (BuildFolder,ProjectName,Critical,High,Medium,Low)

//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

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

Xpath cheatsheet, Text match (substring), //button[contains(text(),"Go")]. Arithmetic Steps of an expression are separated by / , usually used to pick child nodes. That's not always  This allows you to select all book elements that contain a child title element (regardless of how deep it is nested) containing language attribute value equal to 'it'. I cannot say for sure whether or not this answer is relevant to the year 2009 as I am not 100% certain that the XPath Axes existed at that time.

Try this XPath


XPath Cheatsheet --> , 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  In English, that XPath statement translates to “Give me the NAME node that has the text value of DomainController1.MyDomain.demo, that is the childnode of a node called properties, which is a child of one called server, which is the child of a node called group. AND, that group node could be anywhere in the XML.”

Your current xpath should be correct. Here is an alternative but ugly one.

XmlNodeList nodes = doc.SelectNodes("//span/parent::li/span[contains(text(), 'Text1')]/parent::li");

We find all the span-tags. Then we find all the li-tags that has a span-tag as child and contains the 'Text1'.

OR simply:

//span[contains(text(), 'Text1')]/parent::li

Follow the XPath!, '//E[@A="t"]' // element <E> with attribute A containing text 't' exactly (css: '//E[​starts-with(@A, "t")]' // element <E> whose attribute A begins with 't' (css: E' // descendant <E> of element with id I using specific path (css: #I > … > … Since only the root node or element nodes have children, any other use will select nothing. Select Nodes Using XPath Navigation. 03/30/2017; 4 minutes to read +3; In this article. The XML Document Object Model (DOM) contains methods that allow you to use XML Path Language (XPath) navigation to query information in the DOM. You can use XPath to find a single, specific node or to find all nodes that match some criteria. XPath Select Methods

Selecting content on a web page with XPath, XPath is a language that we write to select parts (or nodes) of an Remember, this contains the start and end tags and everything inside: from <element> . . .​contents This link provides a handy picture of how element nodes and text <​body> elements) are the immediate children of the <html> root node. Select node whose child value matches I am trying to select a parent node, that has a child node id that matches a specific value. How would I go about doing this?

XML and XPath, How can I select a specific element on web page? Use the XPath syntax to select elements on this web page You can open an XML document in any text editor and the data it contains will be shown as it is meant to be represented. Text nodes (data) are contained inside the opening and closing tags; XML attribute  XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.

Using XPath to select elements – ParseHub Help Center, XPath can be used to navigate through elements and attributes in an XML XPath contains a library of standard functions; XPath is a major element in XSLT XPath uses path expressions to select nodes or node-sets in an XML /​bookstore/book[1], Selects the first book element that is the child of the bookstore element. The commonly useful XPath axes methods used in Selenium WebDriver are child, parent, ancestor, sibling, preceding, self, etc. XPath axes help to find elements based on the element’s relationship with another element in a document.

  • Your XPath is correct. Which processor are you using, either it does have some serious bug or you might be calling it wrong. As a side note, I think /li[contains(span, 'Text1')] Is a bit more elegant and shorter. You certainly con't need the text(), just use . as it will be automatically converted to an atomic value.
  • Your second XPath does appear correct. Perhaps a namespace issue? Is your XML actually in a namespace?
  • Just for clarification, because it got a few upvotes over the years: it's been a namespace problem.
  • what worked for me in my case is //element1[./childElement[ condition ]]