Hpricot and xpath

Discussion in 'Ruby' started by Li Chen, Aug 12, 2008.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I want to find the path to a tag I am interested using hpricot.
    According to the document and examples I think I should use #xpath
    method.

    I expect that ruby will return the path as this:
    "/tag1/tag2/div[@id='header']"

    but I get the following info:

    tag1.rb:4: undefined method `xpath' for nil:NilClass (NoMethodError)

    I wonder why method #xpath is not defined?

    Thanks,

    Li


    ###########################################
    Here is my code:

    require 'hpricot'
    doc=Hpricot(open('tag.txt'))
    puts doc
    puts doc.at("header").xpath()


    ########tag.txt##########
    <tag1>
    <tag2>
    <div id='header'>
    </tag2>
    </tag1>
    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Aug 12, 2008
    #1
    1. Advertising

  2. Li Chen

    Mark Thomas Guest

    On Aug 12, 2:03 pm, Li Chen <> wrote:
    > Hi all,
    >
    > I want to find the path to a tag I am interested using hpricot.
    > According to the document and examples I think I should use #xpath
    > method.
    >
    > I expect that ruby will return the path as this:
    > "/tag1/tag2/div[@id='header']"
    >
    > but I get the following info:
    >
    > tag1.rb:4: undefined method `xpath' for nil:NilClass (NoMethodError)
    >
    > I wonder why method #xpath is not defined?


    Look carefully. It's not defined for nil. Which means that
    doc.at("header") is returning nil. That's because there are no
    elements with the name of "header"; only an attribute. Try using the
    css selector "#header".
    Mark Thomas, Aug 12, 2008
    #2
    1. Advertising

  3. Li Chen

    Li Chen Guest

    Mark Thomas wrote:
    > Look carefully. It's not defined for nil. Which means that
    > doc.at("header") is returning nil. That's because there are no
    > elements with the name of "header"; only an attribute. Try using the
    > css selector "#header".



    Here is the result after I change to css selector or xpath;

    puts doc.at("#header").xpath()

    ag1.rb:4: undefined method `xpath' for {elem <div id="header"> {text "
    \n" " "}}:Hpricot::Elem (NoMethodError)


    puts doc.at("#header").css_path()

    tag1.rb:4: undefined method `css_path' for {elem <div id="header"> {text
    " \n" " "}}:Hpricot::Elem (NoMethodError)



    Li



    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Aug 12, 2008
    #3
  4. Li Chen

    Mark Thomas Guest

    Are you using an old version of Hpricot? When I run this code:

    #!/usr/bin/env ruby
    require 'rubygems'
    require 'hpricot'
    doc = Hpricot(open("tag.txt"))
    puts doc.at("#header").xpath

    I get the following result:
    //div[@id='header']

    I'm using Hpricot 0.6 (do a gem list --local to see your version)
    Mark Thomas, Aug 12, 2008
    #4
  5. Li Chen

    Li Chen Guest

    Mark Thomas wrote:
    > Are you using an old version of Hpricot? When I run this code:
    >
    > #!/usr/bin/env ruby
    > require 'rubygems'
    > require 'hpricot'
    > doc = Hpricot(open("tag.txt"))
    > puts doc.at("#header").xpath
    >
    > I get the following result:
    > //div[@id='header']
    >
    > I'm using Hpricot 0.6 (do a gem list --local to see your version)


    My version is 0.4. Since I can not install the new version remotely.
    I want to install it locally. But I can't find hpricot gem in Rubyfore.


    Li
    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Aug 12, 2008
    #5
  6. Li Chen

    Li Chen Guest

    Li Chen wrote:
    >>
    >> I'm using Hpricot 0.6 (do a gem list --local to see your version)

    >
    > My version is 0.4. Since I can not install the new version remotely.
    > I want to install it locally. But I can't find hpricot gem in Rubyfore.


    Thank you all for the help.Now it works after install to the 0.6
    version.


    Li

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Aug 12, 2008
    #6
  7. Li Chen

    Li Chen Guest

    Hi all,

    I find that in my code 'xpath' only works if the attribute is 'id'. How
    to explain it?

    Another question: is it possible to get the path/tree relationship of a
    tag(including its attributes)in the following format using hpricot or
    the position of an interested tag within a html page/file:

    tag1/tag2/<div class="header">

    Thanks,

    Li


    ###############tag.txt#######################
    #I change id="header" to class="header"
    <tag1>
    <tag2>
    <div class="header">ABC </div>
    </tag2>
    </tag1>
    tag1.rb:6: undefined method `xpath' for nil:NilClass (NoMethodError)
    >Exit code: 1

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Aug 12, 2008
    #7
  8. Li Chen

    Phlip Guest

    > tag1.rb:6: undefined method `xpath' for nil:NilClass (NoMethodError)

    Do you see the nil in that line? Where is the nil coming from? Is the method
    before the .xpath, on line 6, possibly returning a nil and not an Elem?

    After pondering that, switch '#header' to '.header'!
    Phlip, Aug 13, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,955
    jan V
    Jul 29, 2005
  2. Kenneth McDonald
    Replies:
    6
    Views:
    1,758
    Mark Thomas
    Dec 30, 2008
  3. Phlip
    Replies:
    3
    Views:
    230
    anansi
    Jul 29, 2007
  4. Celine
    Replies:
    13
    Views:
    252
    Chris Shea
    Dec 19, 2007
  5. No Uu
    Replies:
    1
    Views:
    99
    Rob Biedenharn
    May 25, 2009
Loading...

Share This Page