1

I would like catch a text inside td tag but only want the text that is not inside the tag <strong> or <b>.

For instance the case using b tag

<td class="">
    <b>In Care Of Name</b>
     text that I want to catch
</td>

For this case I can retrieve the text using this xpath expression

//td[starts-with(., "In Care Of Name")]//text()[not(ancestor::b)]

and I got the expected result:

text that I want to catch

or the case using strong

<td class="">
        <strong>In Care Of Name</strong>
         text that I want to catch
</td>

For this case I can retrieve the text using this xpath expression

//td[starts-with(., "In Care Of Name")]//text()[not(parent::strong)]

I try to join this two xpath on one using the following expression :

//td[starts-with(., "In Care Of Name")]//text()[not(parent::strong) or not(ancestor::b)]

And I got

In Care Of Name
text that I want to catch

In fact I got two elements of text and that is not what I expected.

Any idea about what is wrong. Do I need change the way to solve this?

Thanks in advance.

Cyberguille
  • 1,552
  • 3
  • 28
  • 58

2 Answers2

1

This XPath,

//td[starts-with(., "In Care Of Name")]/text()

will return the immediate text node children of the td whose string value starts with In Care Of Name:

text that I want to catch

for both of your XML variations involving b and strong children of the td.

See Testing text() nodes vs string values in XPath for further details on the differences between text nodes and string values in XPath.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    But this won't give what you said you wanted ("the text that is not inside the tag `` or ``") if there are other child elements such as `` or ``. – Michael Kay Aug 15 '17 at 14:46
  • @MichaelKay is true, in my specific case my text is not in other child element for this reason work for me, but is good keep in mind that you explain, thanks – Cyberguille Aug 15 '17 at 19:44
1

You want not(A or B) (alternatively, not(A) and not(B)) rather than not(A) or not(B).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • With the solution of @kjhughes I solved my problem and I realized that I don't need to do `OR` in this case, but my real problem is that you explain, a big logic error, thanks a lot for clarify me that's help me a lot, and this is the most big error that I have in my question. – Cyberguille Aug 15 '17 at 19:40