English 中文(简体)
Nokogiri - conservation doctype and meta tag
原标题:Nokogiri -- preserve doctype and meta tags

I m 利用Nokogiri打开一个现有的html档案。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
   <title>Foo</title> 
</head> 
<body>
<!-- stuff -->
</body>
</html>

然后,我改变这个机构的内容:

html_file = Nokogiri::HTML("path/to/html/file")
html_file.css( body ).first.inner_html = "new body content"

然后,我把这一新文件写到这样的档案中:

File.open("path/to/new/html/file",  w ) {|f| f.write html_file}

And this is my resulting html file:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
<html><body>
new body content
</body></html>

我向您提出的问题是,如果能够告诉Nokogiri保存原始文件>>meta 标签,因为似乎在我与Nokogiri开放文件并试图将其书写到档案时,这些标签正在丢失/变。

任何帮助都会受到高度赞赏。 感谢!

问题回答

最后数字如下:

我刚才改变了行文:

html_file = Nokogiri::HTML("path/纽约总部/html/file")

纽约总部

html_file = Nokogiri::HTML(File.open("path/纽约总部/html/file").read)

and now it works like I m expecting it 纽约总部. Seems kind of inconsistent, but I m sure there s a good reason for it.

感谢所有的建议!





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...