<body>
<div>some text</div>
I NEED THIS TEXT ONLY
<div>some text</div>
more text here
<div>some text</div>
one more text here
<div>some text</div>
</body>
How?
<body>
<div>some text</div>
I NEED THIS TEXT ONLY
<div>some text</div>
more text here
<div>some text</div>
one more text here
<div>some text</div>
</body>
How?
I don't have nokogiri, but here's an alternative using just basic string manipulation.
html=<<EOF
<body>
<div>some text</div>
I NEED THIS TEXT ONLY
<div>some text</div>
more text here
<div>some text</div>
one more text here
<div>some text</div>
</body>
EOF
p html.split(/<\/*body>/)[1].split(/<\/div>/)[1].split(/<div>/)[0]
this returns the first text node within body
between two div
elements:
/body/text()[
./preceding::element()[1][local-name()="div"] and
./following::element()[1][local-name()="div"]
][1]
should return
I NEED THIS TEXT ONLY
This XPath 1.0:
/body/text()[preceding-sibling::*[1][self::div]]
[following-sibling::*[1][self::div]][1]
Also:
/body/text()[normalize-space()][1]
Use:
/*/div[1]/following-sibling::text()[1]
This selects the first text-node sibling of the first div
child of the top element of the document.