Get (text) in XPath

76,537

Solution 1

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.

Solution 2

Get the first text node following the first h2 in the div with class "coordsAgence":

div[@class='coordsAgence']/h2[1]/following-sibling::text()[1]

Note that this first expression returns the first text node after the first h2 even when some other node appears between the two. If you want to return the text only when it's the node that immediately follows the first h2, then try something like this:

div[@class='coordsAgence']/h2[1][following-sibling::node()[1][self::text()]]/following-sibling::text()[1]
Share:
76,537
snoofkin
Author by

snoofkin

Developer...............C/C++.......Perl.......XSLT......Bash.......etc.... Concentrating mainly on Perl &amp; C++ and Qt.

Updated on July 09, 2022

Comments

  • snoofkin
    snoofkin almost 2 years

    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.