如何在ruby rxml中逃脱分号

时间:2014-09-28 07:06:15

标签: ruby-on-rails ruby xml

我使用Ruby On Rails' RXML为Google创建站点地图Feed。

Google要求images are marked up with the image name space,要求其中包含分号的元素:

<image:image>
  <image:loc>http://example.com/image.jpg</image:loc> 
</image:image>

如果我使用

xml.image:loc => "something"

我得到了

<image:image>
  <image loc="something"/>
</image:image>

如果我使用

xml.image:loc("something")

我得到了

    compile error
/home/vagrant/website/app/views/feeds/sitemap.rxml:36: syntax error, unexpected '(', expecting kEND
                                            xml.image:loc("something")

如果我试试这个

xml.image:loc do
  puts  "something"
end

我明白了

<image:image>
  <image:loc>
  </image:loc>
</image:image>

1 个答案:

答案 0 :(得分:3)

Rxml模板实际上是builder

如果magic标记方法的第一个参数是符号,那么构建器将使用方法名称作为名称空间,使用符号作为标记名称。例如

xml.image(:loc, "something")

将产生

<image:loc>something</image:loc>

通过街区也是理智的:

xml.image(:image) do |xml|
  xml.image(:loc, "http://example.com")
end