How to create a Nokogiri::XML::Node from Nokogiri::XML::Builder
Asked Answered
F

1

-1

I need to replace a node in a document with new HTML I'm creating.

The class of the node I have to replace is:

Nokogiri::XML::Node

I create my fragment using the Nokogiri Builder:

new_node = Nokogiri::XML::Builder.new do |xml|
  xml.table('border' => '1', 'cellpadding' => '1', 'cellspacing' => '1') {
    xml.thead {
      xml.tr {
        battery_test[0..4].each do |head|
          xml.th_ head["inputValue"]
        end
      }
    }
    xml.tbody {
      battery_test.drop(5).each_slice(5) do |row|
        xml.tr {
          row.each do |item|
            xml.td_ item["inputValue"]
          end
        }
      end
    }
  }
end

But the class of new_node is Nokogiri::XML::Builder.

How can I replace my Nokogiri::XML::Node with the fragment I create with the builder?

Fitz answered 21/2, 2020 at 0:20 Comment(1)
Please see "MCVE". We need the smallest runnable code that duplicates the along with the desired output in the question itself. Without that we're shooting in the dark. For instance, what is battery_test?Riley
R
1

You don't have to use Builder to create nodes. Nokogiri allows several ways of defining them. Your question isn't asked well as it's missing essential information, but this will get you started:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<html>
  <head></head>
  <body>
  </body>
</html>
EOT

puts doc.to_html

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >>   <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
# >>   <body>
# >>   </body>
# >> </html>

I can add a table using a string containing the HTML:

body = doc.at('body')
body.inner_html = "<table><tbody><tr><td>foo</td><td>bar</td></tr></tbody></table>"

puts doc.to_html

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >>   <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
# >>   <body><table><tbody><tr>
# >> <td>foo</td>
# >> <td>bar</td>
# >> </tr></tbody></table></body>
# >> </html>

Modify the string generation to contain the HTML you need, let Nokogiri do the heavy lifting, and you're done. It's easier to read and maintain.

inner_html= is defined as:

inner_html=(node_or_tags)

node_or_tags means you can pass a node created using Builder, snipped from some other place in the DOM, or a string containing the markup.

Similarly:

table = Nokogiri::XML::Node.new('table', doc)
table.class # => Nokogiri::XML::Element

table.add_child('<tbody><tr><td>foo</td><td>bar</td></tr></tbody>')
body = doc.at('body')
body.inner_html = table

puts doc.to_html

# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >>   <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
# >>   <body><table><tbody><tr>
# >> <td>foo</td>
# >> <td>bar</td>
# >> </tr></tbody></table></body>
# >> </html>

Note that table is a Nokogiri::XML::Element. HTML nodes are a subclass of XML nodes so don't let that confuse you.

The tutorials are good starting points for trying anything with Nokogiri. In this case "Modifying an HTML / XML Document" is useful. Also the "Cheat sheet" is chock-full of goodness. Finally, "Questions tagged [nokogiri]" reveals all the top questions on Stack Overflow.

Riley answered 23/2, 2020 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.