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

45,854

Solution 1

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

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

Solution 2

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

Solution 3

Try this XPath

li/*[@innertext='text']

Solution 4

the xpath Kent Kostelac provided:

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

could be also be presented as so:

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

which is a bit shorter and provides improved readability by saying "give me the li that contains the span that displays 'Text1'"; instead of saying "can you see that span that dispalys 'Text1'? ok, so give me its li parent", which is what the original phrasing is actually saying.

Solution 5

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

Share:
45,854

Related videos on Youtube

D.R.
Author by

D.R.

https://www.rauch.io/

Updated on April 19, 2022

Comments

  • D.R.
    D.R. about 2 years

    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?

    • dirkk
      dirkk almost 10 years
      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.
    • adamretter
      adamretter almost 10 years
      Your second XPath does appear correct. Perhaps a namespace issue? Is your XML actually in a namespace?
    • D.R.
      D.R. over 4 years
      Just for clarification, because it got a few upvotes over the years: it's been a namespace problem.
  • Bhanu Chhabra
    Bhanu Chhabra about 5 years
    what worked for me in my case is //element1[./childElement[ condition ]]
  • BergListe
    BergListe over 3 years
    This is a bit OT but I really want to thank you for your first screen shot from which I learned that one could do XPath queries in Chrome's JS console!