使用Nokogiri时,如何抑制自动关闭标签的插入?
问题描述:
我的XML文档是Jenkins作业的配置文件,其中包含许多空标签,例如:
My XML doc, which is the config file for a Jenkins job, has a lot of empty tags like:
<string></string>
Nokogiri替换为:
which Nokogiri replaces with:
<string/>
尽管这是编写XML的推荐"方法,但最终会导致对XML产生不必要的更改,从而使得难以读取有意义的内容更改.有没有办法抑制这种行为?
While this is the "recommended" way to write XML, it ends up generating unnecessary changes to the XML that make it difficult to read the meaningful content changes. Is there a way to suppress this behavior?
答
您可以使用 NO_EMPTY_TAGS
选项:
You can use the NO_EMPTY_TAGS
option:
doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)
或更简洁:
doc.to_xml &:no_empty_tags