如何使用元素名称中带有连字符的 Nokogiri::XML::Builder 创建 XML? [英] How do I create XML using Nokogiri::XML::Builder with a hyphen in the element name?

查看:40
本文介绍了如何使用元素名称中带有连字符的 Nokogiri::XML::Builder 创建 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Nokogiri 构建一个 XML 文档.某些元素中带有连字符.举个例子:

I am trying to build an XML document using Nokogiri. Some of the elements have hyphens in them. Here's an example:

require "nokogiri"
builder = Nokogiri::XML::Builder.new do |xml|
  xml.foo_bar "hello"
end

puts builder.to_xml

产生:

<?xml version="1.0"?>
<foo_bar>hello</foo_bar>

但是,当我尝试时:

builder = Nokogiri::XML::Builder.new do |xml|
  xml.foo-bar "hello"
end

我明白了:

syntax error, unexpected tSTRING_BEG, expecting kDO or '{' or '('
  xml.foo-bar "hello"

现在我意识到这是因为连字符被解释为 foo MINUS bar.

Now I realise this is because the hyphen is being interpreted as foo MINUS bar.

我该怎么做?

推荐答案

给你:

require 'nokogiri'

b = Nokogiri::XML::Builder.new do |xml|
  xml.send(:"fooo-bar", "hello")
end

puts b.to_xml

这篇关于如何使用元素名称中带有连字符的 Nokogiri::XML::Builder 创建 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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