Search code examples
htmldomxpathhtml-parsing

Get (text) in XPath


I have the following DOM structure / HTML, I want to get (just practicing...) the marked data. enter image description here

The one that is under the h2 element. that div[@class="coordsAgence"] element, has some more div children below and some more h2's.. so doing:

div[@class="coordsAgence"]

Will get that value, but with additional unneeded text. UPDATE: The value (From this example) that I basically want is that: "GALLIER Dennis" text.


Solution

  • It seems you want the first text node in that div:

    div[@class="coordsAgence"]/text()[1]
    

    should do it.

    Note that this assumes that there is actually no whitespace between those comments inside <div class="coordsAgence">; otherwise that whitespace will constitute additional text nodes that you'll have to account for.