Xpath - get only node content without other elements
36,664
Solution 1
To get the string value of div
use:
string(/div)
This is the concatenation of all text nodes that are descendents of the (top) div
element.
To select all text node descendents of div
use:
/div//text()
To get only the text nodes that are direct children of div
use:
/div/text()
Finally, get the first (and hopefully only) non-whitespace-only text node child of div
:
/div/text()[normalize-space()][1]
Solution 2
expression like ./text() will retrieve only the content of root element only.
Regards, Nitin
Solution 3
You can use this XPath expression:
./div[1]/text()[1]
to test, I use this online tester : http://xpather.com/
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on April 20, 2021Comments
-
Admin about 3 years
I have an
div
elemet:<div> This is some text <h1>This is a title</h1> <div>Some other content</div> </div>
What xpath expression should I use to only get the
div
content without his child elementsh1
anddiv
//div[not(h1)¬(div)]
Something like that? I cannot figure it out
-
Eduard Sukharev over 4 yearsYour answer only shows how to get text of either parent node AND it's descendants, or descendants only. I'm sure the question was how to get ONLY parent node text, without descendants (e.g.
This is some text
in example given in question) -
Dimitre Novatchev over 4 years@EduardSukharev, The answer correctly provides this expression:
/div/text()
In general one may not know exactly which of the text-node children ofdiv
is the wanted one. There may be other, whitespace-only nodes. So, one has to provide the position of the node they are interested in... In case it is specified that only one text child node ofdiv
is a non-whitespace-only node, then the expression to select this node is:/div/text()[not(normalize-space())][1]
I have edited the question. Please reverse your downvote and consider upvoting now. -
Eduard Sukharev over 4 yearsMy bad, I misunderstood
text nodes
as justnodes
, which is obviously not what you meant. And, yes, your answer is correct, full and thorough. Thank you.