Xpath syntax to grab text data after strong tag containing header text

<li><strong>Movie Title:</strong> Training Day</li>

      

How to grab the text content in this li tag; "Learning Day"?

So I need to say, "If the strong tag has" Movie Title "return" Learning Day ".

I've tried things using "next brother" but can't seem to figure it out.

Another try -

//li/text()[preceding::strong[contains(text(),'Movie Title')]] 

      

But this returns ALL text, not just what is inside the li class.

+3


source to share


2 answers


How to grab the text content in this li tag; "Learning Day"

So I need to say, "If the strong tag has a Movie Title, return" Learning Day ".

The following XPath expression selects all li-element text nodes after the strong element whose value contains the string Movie Title.

//li[contains(strong,'Movie Title')]/strong/following-sibling::text()

      



In your XML example, this is the "Learning Day" result.
But if additional text-nodes follow, you will need to constrain the expression to the first text-node like this

//li[contains(strong,'Movie Title')]/strong/following-sibling::text()[1]

      

0


source


In fact, it //li/text()

should return "Training day", but //li//text()

- both "Training day" and "Movie title:"

You can try more specifically XPath



//li[starts-with(., "Movie Title:")]//text()[not(parent::strong)]

      

to get only "Training Day"

+2


source







All Articles