ruby - 如何在 Nokogiri 中创建具有长命名空间的 XML,除以点

标签 ruby xml namespaces nokogiri

我需要在 Nokogiri 中重新创建这部分 XML 文档:

<propertiesJMeter>
    <jmeter.save.saveservice.thread_counts>
       true
    </jmeter.save.saveservice.thread_counts>
</propertiesJMeter>

我像这样用 Nokogiri::XML::Builder 构建它,这里只是有问题的部分:

propertiesJMeter {
  jmeter.save.saveservice.thread_counts 'true'
}

结果不正确,因为我不需要那个类变量并且我想保存命名空间结构:

<propertiesJMeter>
  <jmeter class="save saveservice thread_counts">
     true
  </jmeter>
</propertiesJMeter>

最佳答案

发布“nokogiri-talk”组的答案,这对我有帮助

The issue is that

jmeter.save

is interpreted by Nokogiri Builder as a "Tag Attribute Short Cut" (as you can read about at http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Builder).

To make a tag with a "." in the tagname, you'll need to make sure ruby doesn't interpret the "." as an instance method call. This works:

propertiesJMeter {
  send "jmeter.save.saveservice.thread_counts", 'true'
}

and emits

<propertiesJMeter>
  <jmeter.save.saveservice.thread_counts>true</jmeter.save.saveservice.thread_counts>
</propertiesJMeter>

关于ruby - 如何在 Nokogiri 中创建具有长命名空间的 XML,除以点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32839813/

相关文章:

ruby - 用于使用 ruby​​ 大量插入 redis 的 ram 文件?

html - 为什么 CSS 类被忽略了?

ruby - 基于不同权重随机打乱数组的算法

xml - XSLT - 如何合并 XSLT 文件

c# - 在 C# 中根据 XSD 模式验证 json 数据

WCF 数据服务代理不想两次使用相同的命名空间

python - 嵌套函数的名称是什么?为什么 eval 看不到嵌套函数?

Ruby IO.popen 带有 "-",幕后发生了什么?

java - 从 Java 排序和比较 XML

xml - 反 XML scala 隐藏 xml 属性