简体   繁体   English

Nokogiri - 如何命名节点`comment`?

[英]Nokogiri - How to name a node `comment`?

I am using Nokogiri to create some XML: 我正在使用Nokogiri创建一些XML:

def builder
  Nokogiri::XML::Builder.new do |xml|
    xml.foobar do
      xml.comment('Some comment', created_at: Time.zone.now.iso8601)
    end
  end
end

I want this structure: 我想要这个结构:

<foobar>
  <comment created_at='...'>
   Some comment
  </comment>
</foobar>

Unfortunately the Nokogiri DSL won't allow me to name a node comment since it's an internal method to create XML-Comments. 不幸的是,Nokogiri DSL不允许我命名节点comment因为它是创建XML-Comments的内部方法。 How can I achieve this? 我怎样才能做到这一点?

From the documentation: http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Builder 来自文档: http//www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Builder

Unfortunately some methods are defined in ruby that are difficult or dangerous to remove. 不幸的是,一些方法在红宝石中定义,难以去除或有危险。 You may want to create tags with the name “type”, “class”, and “id” for example. 例如,您可能想要创建名称为“type”,“class”和“id”的标签。 In that case, you can use an underscore to disambiguate your tag name from the method call. 在这种情况下,您可以使用下划线来消除方法调用中的标记名称的歧义。

Therefore you need: 因此你需要:

def builder
  Nokogiri::XML::Builder.new do |xml|
    xml.foobar do
      xml.comment_('Some comment', created_at: Time.zone.now.iso8601)
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM