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/

Share:
36,664
Admin
Author by

Admin

Updated on April 20, 2021

Comments

  • Admin
    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 elements h1 and div

    //div[not(h1)&not(div)]

    Something like that? I cannot figure it out

  • Eduard Sukharev
    Eduard Sukharev over 4 years
    Your 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
    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 of div 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 of div 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
    Eduard Sukharev over 4 years
    My bad, I misunderstood text nodes as just nodes, which is obviously not what you meant. And, yes, your answer is correct, full and thorough. Thank you.