Re: データ抽出クラス(xpath で取得する場合)

以下の使用サンプル。
データ抽出クラス - A More Beautiful day


シンボル :xpath を使って、以下のように指定する。
また :expr を指定することで、一致したノード以下のテキストや属性値を
取得することができる。

text 一致したノードが持つテキストを返す
inner_txt 一致したノード以下のテキストを返す
@属性名 一致したノードの属性値を返す

以下のようなデータがあるとすると

<root>
  <title>extract test</title>

  <record>
    <item>
      <date tz='jp'>2007/11/28</date>
      <foo>000</foo>
    </item>
    <item>
      <date tz='utc'>2007/11/27</date>
      <foo>000<bar>111</bar>222</foo>
    </item>
  </record>
</root>

以下のような ruby ソースコード

    data = Extract.match :text => @text do
      summary :title => { :xpath => 'title' }

      record :xpath => 'item' do
        item :name    => { :xpath => 'name' }
        item :id      => { :xpath => 'id' }
        item :date    => { :xpath => 'date' }
        item :comment => { :xpath => 'comment' }
      end
    end

以下のようなハッシュデータを取得できる

{
 :summary=>{:title=>"extract test"},
 :item=>
  [{:test1=>"000", :test2=>"000", :test3=>""},
   {:test1=>"000222", :test2=>"000111222", :test3=>""}]
}