0

在我的控制器中,我有:

xml_doc.xpath('//locale').map do |i|
Thing.create!(:name => i.xpath('englishName').inner_text, :lang => i.xpath('representation').inner_text)
end

xml 文档是: http: //www.facebook.com/translations/FacebookLocales.xml

问题是 lang 列设置为:

af_ZAar_ARaz_AZbe_BYbg_BGbn_INbs_BAca_EScs_CZcy_G...

对于创建的每个元素。

问题: 在此处输入图像描述

4

2 回答 2

3

最终的问题是 与representation不在同一级别englishName

xml_doc.xpath('//locale').map do |i|
  englishName = i.xpath('englishName').inner_text

  # Or just ".//representation", or if there may be multiple codes, you
  # need to be even more clever about determining which to use.
  representation = i.xpath('codes/code/standard/representation').inner_text

  puts "#{englishName} - #{representation}"
end

输出(截断):

Thai - th_TH
Filipino - tl_PH
Turkish - tr_TR
Ukrainian - uk_UA
Vietnamese - vi_VN
Simplified Chinese (China) - zh_CN
Traditional Chinese (Hong Kong) - zh_HK
Traditional Chinese (Taiwan) - zh_TW
于 2012-06-07T10:58:14.483 回答
2

只需稍作改动即可修复它:

xml_doc.xpath('//locale').map do |i|
    Thing.create!(:name => i.xpath('englishName').inner_text, :lang => i.xpath('.//representation').inner_text)
end

'representation'改为'.//representation'因为representation不是locale. 我刚刚对其进行了测试,并且效果很好。

于 2012-06-07T10:46:33.473 回答