Xpath - get only node content without other elements

后端 未结 4 1967
暖寄归人
暖寄归人 2020-12-14 06:20

I have an div elemet:

This is some text

This is a title

Some other content
<
相关标签:
4条回答
  • 2020-12-14 06:34

    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()[not(normalize-space())][1]
    
    0 讨论(0)
  • 2020-12-14 06:35

    You can use this XPath expression:

    ./div[1]/text()[1]
    

    to test, I use this online tester : http://xpather.com/

    0 讨论(0)
  • 2020-12-14 06:37

    expression like ./text() will retrieve only the content of root element only.

    Regards, Nitin

    0 讨论(0)
  • 2020-12-14 06:42

    What xpath expression should I use to only get the div content without his child elements h1 and div

    This XPath expression:

    /div/node()[not(self::h1|self::div)]
    

    It selects every div root element's children except those h1 or div elements.

    0 讨论(0)
提交回复
热议问题