Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a dtd using nokogiri builder

I am using nokogiri to generate svg pictures. I would like to add the correct xml preamble and svg DTD declaration to get something like:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg>
...

With builder I could use instruct! and declare! but I want to stick with nokogiri because I use it for other purpose in my project and I want to stay low on requirements. Do you have some ideas ?

Thanks

like image 225
paradigmatic Avatar asked Dec 25 '09 16:12

paradigmatic


2 Answers

Following is from a note at the bottom of the Nokogiri::XML::Builder page (maybe added recently), which I think will do the trick:

builder = Nokogiri::XML::Builder.new do |xml|
  xml.doc.create_internal_subset(
    'html',
    "-//W3C//DTD HTML 4.01 Transitional//EN",
    "http://www.w3.org/TR/html4/loose.dtd"
  )
  xml.root do
    xml.foo
  end
end

puts builder.to_xml
like image 120
Eric Walker Avatar answered Oct 15 '22 14:10

Eric Walker


You can now (don't know from which version) use Node#create_internal_subset to create the DTD node. For more info see: http://nokogiri.org/Nokogiri/XML/Builder.html

And scroll down to the "Document Types" section for an example.

like image 2
Pavel Kunc Avatar answered Oct 15 '22 14:10

Pavel Kunc