5

我正在尝试使用 ruby​​ 进行 Google kml 之旅,但此代码出现语法错误

xml = builder.gx:Tour

它不喜欢结肠。有没有办法强制它编译这个?

4

5 回答 5

9

不得不做

xml.tag!("gx:tour")
于 2009-12-03T02:45:10.650 回答
4

如果您想在标签内添加另一个标签,那么

xml.tag!("tag:name", attribute: "value") do |t|
   t.title("value for title")
end

如果你想输入一个简单的值,那么

xml.tag!("tag:name","value for tag", attribute: "attribute value")
于 2013-03-22T07:16:00.053 回答
4

是的,如果你想增加一些价值,那就是

xml.tag!("gx:tour", "value of gx:tour", "attribute1"=>"attribute1val", "attribute2"=>"attribute2val", ..., "attributeN"=>"attributeNval") 
于 2010-02-07T07:33:24.497 回答
1

由于 Builder 的第 2 版有一些对命名空间的支持

所以现在如果你想达到同样的效果,你可以在冒号前添加一个空格:

xml = builder.gx :Tour
于 2012-06-25T13:40:24.423 回答
0

超级老问题,但在带有 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
于 2018-03-30T12:57:33.560 回答