73

I'm trying to extract an element with a particular innertext from a parsed XML document. I know that I can select an element that has a child with a particular innertext using //myparent[mychild='foo'], but I actually just want to select the "mychild" element in this example.

<myparent>
  <mychild>
    foo
  </mychild>
</myparent>

What would be the XPath query for "foo" that would return the "mychild" node?

4 Answers 4

112

Have you tried this?

//myparent/mychild[text() = 'foo']

Alternatively, you can use the shortcut for the self axis:

//myparent/mychild[. = 'foo']
Sign up to request clarification or add additional context in comments.

2 Comments

or //myparent[mychild = 'foo']
@Matt : if OP wants to select the mychild element, then your solution won't work, it will select the myparent element.
9

You might consider using the contains function to return true/false if the test was found like so:

//mychild[contains(text(),'foo')]

See XSLT, XPath, and XQuery Functions for functions reference

2 Comments

+1: in my case //h3[contains(text(),"In This Week’s Podcast")] found element and //h3[text()="In This Week’s Podcast"] did not; I only tested it on Chrome.
To Marcelo Scofano Diniz: yes, because text() has to exactly equal with the string. Any leading or closing space, pagebreak, tab or whitespace character makes that to false.
7

Matt said it, but the full solution:

//myparent[mychild='foo']/mychild

1 Comment

it searches all child nodes of "myparent" where any "mychild" of "myparent" has "foo" as inner text.
3

As per the HTML:

<myparent>
  <mychild>
    foo
  </mychild>
</myparent>

The <mychild> element with text as foo is within it's parent <myparent> tag and the text contains leading and trailing white space characters.

So to select the <mychild> element you can use either of the following solutions:

  • Using normalize-space():

    //myparent/mychild[normalize-space()='foo']
    
  • Using contains():

    //myparent/mychild[contains(., 'foo')]
    

Ignoring the parent <myparent> tag you can also use:

  • Using normalize-space():

    //mychild[normalize-space()='foo']
    
  • Using contains():

    //mychild[contains(., 'foo')]
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.