RubyXMLBuilder中的特殊字符
我正在尝试使用ruby进行GoogleKML之旅,我发现这段代码有语法错误RubyXMLBuilder中的特殊字符,xml,ruby,builder,Xml,Ruby,Builder,我正在尝试使用ruby进行GoogleKML之旅,我发现这段代码有语法错误 xml = builder.gx:Tour 它不喜欢冒号。有没有办法强迫它编译这个?必须这样做 xml.tag!("gx:tour") 是的,如果你想给它一些价值,它会是这样的 xml.tag!("gx:tour", "value of gx:tour", "attribute1"=>"attribute1val", "attribute2"=>"attribute2val", ..., "attribu
xml = builder.gx:Tour
它不喜欢冒号。有没有办法强迫它编译这个?必须这样做
xml.tag!("gx:tour")
是的,如果你想给它一些价值,它会是这样的
xml.tag!("gx:tour", "value of gx:tour", "attribute1"=>"attribute1val", "attribute2"=>"attribute2val", ..., "attributeN"=>"attributeNval")
自Builder的第2版开始,就有了 因此,如果您想获得相同的结果,可以在冒号之前添加空格:
xml = builder.gx :Tour
如果要在标记内添加另一个标记,则
xml.tag!("tag:name", attribute: "value") do |t|
t.title("value for title")
end
如果你想要一个简单的值,那么
xml.tag!("tag:name","value for tag", attribute: "attribute value")
超级古老的问题,但在Rails 5.1.5和Builder 3.2.3中,使用名称空间、嵌套等非常简单。下面是一个精心设计的示例,但我认为它显示了所有不同的组合:
<?xml version="1.0" encoding="UTF-8"?>
<root simple="foo" xmlns:example="http://www.example.com/example.dtd">
<container>
<element>A normal element</element>
<example:namespaced_element>An element in the "example" namespace</example:namespaced_element>
</container>
<example:namespaced_container>
<element_with_attribute attribute="foo">Another element</element_with_attribute>
<example:namespaced_element_with_attribute attribute="bar">Another namespaced element</example:namespaced_element_with_attribute>
</example:namespaced_container>
<container_with_attribute attribute="baz">
<empty_element/>
<example:namespaced_empty_element/>
</container_with_attribute>
<example:namespaced_container_with_attribute attribute="qux">
<empty_element_with_attribute attribute="quux"/>
<example:namespaced_empty_element_with_attribute attribute="corge"/>
</example:namespaced_container_with_attribute>
</root>
#encoding: UTF-8
xml.instruct! :xml, version: '1.0'
xml.root simple: 'foo', 'xmlns:example': 'http://www.example.com/example.dtd' do
xml.container do
xml.element 'A normal element'
xml.example :namespaced_element, 'An element in the "example" namespace'
end
xml.example :namespaced_container do
xml.element_with_attribute 'Another element', attribute: 'foo'
xml.example :namespaced_element_with_attribute, 'Another namespaced element', attribute: 'bar'
end
xml.container_with_attribute attribute: 'baz' do
xml.empty_element
xml.example :namespaced_empty_element
end
xml.example :namespaced_container_with_attribute, attribute: 'qux' do
xml.empty_element_with_attribute attribute: 'quux'
xml.example :namespaced_empty_element_with_attribute, attribute: 'corge'
end
end