Nokogiri text node contents

前端 未结 2 2078
感动是毒
感动是毒 2021-01-05 00:45

Is there any clean way to get the contents of text nodes with Nokogiri? Right now I\'m using

some_node.at_xpath( \"//whatever\" ).first.content
2条回答
  •  没有蜡笔的小新
    2021-01-05 00:58

    Just look for text nodes:

    require 'nokogiri'
    
    doc = Nokogiri::HTML(<
    
    

    This is a text node

    This is another text node

    EOT doc.search('//text()').each do |t| t.replace(t.content.strip) end puts doc.to_html

    Which outputs:

    
    
    

    This is a text node

    This is another text node

    BTW, your code example doesn't work. at_xpath( "//whatever" ).first is redundant and will fail. at_xpath will find only the first occurrence, returning a Node. first is superfluous at that point, if it would work, but it won't because Node doesn't have a first method.


    I have bar, how I get at the "bar" text without doing doc.xpath_at( "//data/foo" ).children.first.content?

    Assuming doc contains the parsed DOM:

    doc.to_xml # => "\n\n  bar\n\n"
    

    Get the first occurrence:

    doc.at('foo').text       # => "bar"
    doc.at('//foo').text     # => "bar"
    doc.at('/data/foo').text # => "bar"
    

    Get all occurrences and take the first one:

    doc.search('foo').first.text      # => "bar"
    doc.search('//foo').first.text    # => "bar"
    doc.search('data foo').first.text # => "bar"
    

提交回复
热议问题