我正在尝试使用 ruby 进行 Google kml 之旅,但此代码出现语法错误
xml = builder.gx:Tour
它不喜欢结肠。有没有办法强制它编译这个?
不得不做
xml.tag!("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")
是的,如果你想增加一些价值,那就是
xml.tag!("gx:tour", "value of gx:tour", "attribute1"=>"attribute1val", "attribute2"=>"attribute2val", ..., "attributeN"=>"attributeNval")
超级老问题,但在带有 Builder 3.2.3 的 Rails 5.1.5 中,使用命名空间、嵌套等非常简单。以下是一个人为的示例,但我认为它显示了所有不同的组合:
<?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