When using Nokogiri, how do you suppress the insertion of self-closing tags?

穿精又带淫゛_ 提交于 2019-12-10 18:15:58

问题


My XML doc, which is the config file for a Jenkins job, has a lot of empty tags like:

<string></string>

which Nokogiri replaces with:

<string/>

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?


回答1:


You can use the NO_EMPTY_TAGS option:

doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)

or the rather more concise:

doc.to_xml &:no_empty_tags


来源:https://stackoverflow.com/questions/26416406/when-using-nokogiri-how-do-you-suppress-the-insertion-of-self-closing-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!