在使用from_xml转换为哈希时,如何使用XML属性?

我有一个XML元素,其中包含多个使用相同ID的子元素。 子元素可通过“ size ”属性区分。 当我使用from_xml创建一个哈希时,它会将所有这些子元素1字段from_xml

如何根据XML属性将这些子元素解析到不同的字段?

XML:

  KISS http://userserve-ak.last.fm/serve/34/3329738.jpg http://userserve-ak.last.fm/serve/64/3329738.jpg http://userserve-ak.last.fm/serve/126/3329738.jpg http://userserve-ak.last.fm/serve/252/3329738.jpg http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg  

ruby:

 >>xml = "KISShttp://userserve-ak.last.fm/serve/34/3329738.jpghttp://userserve-ak.last.fm/serve/64/3329738.jpghttp://userserve-ak.last.fm/serve/126/3329738.jpghttp://userserve-ak.last.fm/serve/252/3329738.jpghttp://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg" => "KISShttp://userserve-ak.last.fm/serve/34/3329738.jpghttp://userserve-ak.last.fm/serve/64/3329738.jpghttp://userserve-ak.last.fm/serve/126/3329738.jpghttp://userserve-ak.last.fm/serve/252/3329738.jpghttp://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg" >>h = Hash.from_xml xml => {"artist"=>{"name"=>"KISS", "rank"=>"48", "image"=>["http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "http://sofzh.miximages.com/ruby-on-rails/KISS+04.jpg"]}} 

我想要的是:

 => {"artist"=>{"name"=>"KISS", "rank"=>"48", "image_small"=>"http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "image_medium"="http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "image_large"="http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", "image_extralarge"="http://sofzh.miximages.com/ruby-on-rails/3329738.jpg", image_mega"="http://sofzh.miximages.com/ruby-on-rails/KISS+04.jpg"}} 

Hash.to_xml目前不会这样做。 它真的只适用于最简单的情况。

但是,有一些打开的票,有一些建议的补丁。 不过,我会推荐像Nokogiri这样的东西来解析你的XML。