user3351503
user3351503

Reputation: 23

Xpath select only the text of tag that has more childs

I have this Block in the page

    <h3 style="line-height:28px; margin-bottom:0px">
    <span style=" font-size:12px; background-color:#0050A1; padding:0 8px 0 8px;"> 
<a style="color:#FFF;">xxxxxx</a>
    </span>
    yyyyy
    </h3>

I want to have yyyyy
I tried

 //h3[@style='line-height:28px; margin-bottom:0px']text() 
 //h3[@style='line-height:28px; margin-bottom:0px']/following-sibling::text()
 //h3[@style='line-height:28px; margin-bottom:0px']//*[not(child::span)]
 //h3[@style='line-height:28px; margin-bottom:0px']text()[last()]

But nothing ....

Upvotes: 2

Views: 143

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

For the snippet you show, the following simple path expression would do:

//h3/text()[2]

which translates to

//h3            Find h3 elements anywhere in the document
/text()         Find all text nodes of all `h3` elements
[2]             Only return the second of those text nodes

which yields

⏎ (a newline character)
yyyyy

If you need the h3 element to be more specific, use something like

//h3[@style = 'line-height:28px; margin-bottom:0px']/text()[2]

But of course, you do not show much of your HTML and I can only guess what other content there might be.


Besides, your question title:

Xpath select only the text of tag that has more childs

is pretty incomprehensible. I have no idea how it relates to retrieving "yyyyy" from the document.

Upvotes: 1

Related Questions