From: tamouse mailing lists Date: 2013-04-15T02:03:34+09:00 Subject: Re: Why does #content method in nokogiri not printing the full text? On Sun, Apr 14, 2013 at 11:59 AM, tamouse mailing lists wrote: > On Sun, Apr 14, 2013 at 11:19 AM, Love U Ruby wrote: >> Here is the documentation: http://www.rubydoc.info/gems/nokogiri/frames >> >> Why does below code not printing the full text? >> >> Code: >> ====== >> >> require 'nokogiri' >> >> html = <<-END >> >> >> >> >> A Dirge >> >> > href = "http://purl.org/DC/elements/1.0/"> >> >> > content = "A Dirge"> >> >> > content = "Shelley, Percy Bysshe"> >> >> > content = "poem"> >> >> > content = "1820"> >> >> > content = "text/html"> >> >> > content = "en"> >> >> >> >>
>>
>>             Rough wind, that moanest loud
>>               Grief too sad for song;
>>             Wild wind, when sullen cloud
>>               Knells all the night long;
>>             Sad storm, whose tears are vain,
>>             Bare woods, whose branches strain,
>>             Deep caves and dreary main, -
>>               Wail, for the world's wrong!
>>
>>     
>> >> >> END >> >> doc = Nokogiri::HTML::DocumentFragment.parse(html) >> doc.children.each do |ch| >> p ch.content if ch.text? >> end >> >> Output: >> ======== >> >> "\n\n \n\n " >> "\n\n " >> >> Expected output: >> ================ >> >> Rough wind, that moanest loud >> Grief too sad for song; >> Wild wind, when sullen cloud >> Knells all the night long; >> Sad storm, whose tears are vain, >> Bare woods, whose branches strain, >> Deep caves and dreary main, - >> Wail, for the world's wrong! >> >> -- >> Posted via http://www.ruby-forum.com/. >> > > If you actually look at the structure of doc, the next to last entry > in it's children contains children as well, which you need to loop > through. Try this: > > (load your code into irb) > require 'pp' > pp doc > > and see what the structure is. > Follow-up: since you have a complete html document, why treat it as a fragment? You can call Nokogiri::HTML.parse(html) instead and get the actual complete document tree with all the proper nesting.