using XPath: how to exclude text in nested elements

前端 未结 2 1962
礼貌的吻别
礼貌的吻别 2021-01-13 01:49

if I have some html like the following

Game Title

相关标签:
2条回答
  • 2021-01-13 02:09

    The conditions in square brackets ("predicate") specify conditions for the node. The div node is not h1 at the same time, so the negation is satisfied. But if you used child instead of self, which was probably your original intent, you would not get the expected text - you would get nothing, because it means "Search for a div with unique_id tah does not have a h1/span child".

    If you want text, specify text():

    //div/text()[last()]
    
    0 讨论(0)
  • 2021-01-13 02:27
    div[@class = 'unique_id']/text()[not(normalize-space() = '')]
    

    or

    div[@class = 'unique_id']/text()[last()]
    

    depending on context.

    Note that you still have to trim the resulting text node.

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