我使用Rails 2.1.0和Nokogiri 1.6.1。我想要的東西似乎很簡單。我想我的Rails REST API與這樣的元素返回XML:如何在Nokogiri中創建具有屬性和值的xml元素
<PeopleNumber unit="NumberOfPeople">2.235075</PeopleNumber>
我試着寫類似:
xml = Nokogiri::NML::Builder.new do |xml|
xml.PeopleNumber(:unit => "NumberOfPeople") 2.235075
的ActionController打完一個語法錯誤。
如果我嘗試重新寫這爲
xml = Nokogiri::NML::Builder.new do |xml|
xml.PeopleNumber(:unit => "NumberOfPeople") { 2.235075 }
我得到類似
<PeopleNumber unit="NumberOfPeople" />
有誰知道的一種方式來獲得在引入nokogiri所需的行爲?
請檢查您的代碼,並將其格式正確,以便我們提供幫助。 http://stackoverflow.com/help/formatting – Ernest 2014-09-30 10:13:54
可能重複的[設置標籤屬性和添加純文本內容使用nokogiri生成器(紅寶石)標籤](http://stackoverflow.com/questions/16219343/set-tag -attribute-and-add-plain-text-content-to-the-tag-using-nokogiri-builder) – 2014-09-30 11:15:32
謝謝@JustinKo。你的解決方案效果很好很容易,一旦你有解決方案! – user1327955 2014-10-01 05:40:06