使用Nokogiri时,如何抑制自动关闭标签的插入? [英] When using Nokogiri, how do you suppress the insertion of self-closing tags?

查看:135
本文介绍了使用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

这篇关于使用Nokogiri时,如何抑制自动关闭标签的插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆