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'representationlocale

于 2012-06-07T10:46:33.473 に答える