blabla
thi...">

XPath language selection

I have this HTML / XML:

\t\t\t\t\t \r\n\t\t <a href="/test.aspx"> <span class=test> <b>blabla</b> </span> </a> <br/> this is the text I want <br/> <span class="test"> <b>code: 123</b> </span> <br/> <span class="test"></span> \t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t 

In C # 4, I use the HtmlAgilityPack lib to select Node with XPath and get the InnerText property. This will get all the text inside the node. How can I get only the text "this is the text I want"?

/text() returns only \t\t\t\t\t \r\n\t\t

+4
source share
3 answers
 /div/text() 

From the above example, this XPath will get all text nodes under the div element, in this case test2.

If you could tell more about this issue, we could better help you. Div contains 3 children: span element, text node and element b. Each range and b have a child text node. Using XPath, you can select only elements (/ div / *), only text nodes (/ div / text ()), or all node types (/ div / node ()).

EDIT: / text () will return only root level text nodes. In this case, I expect it to return a node list containing 3 text nodes:

 \t\t\t\t\t \r\n\t\t this is the text I want \t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t 

Perhaps you select only the first node in the resulting node list? There are several problems of the correct form, for example, your <br> should be <br/> .

+10
source

@peter: You do not have to edit your question so that people do not see how the accepted answer relates to the question !!!

The answer to your new question:

 /br[1]/following-sibling::text()[1] 

selects the desired text node (my quotation marks):

 " this is the text I want " 
+1
source

How can I get only the text "this is the text I want"?

 text()[preceding-sibling::node()[1][self::br]] [following-sibling::node()[1][self::br]] 

Value: node text between two br elements.

0
source

All Articles