views:

72

answers:

1

any idea how I can pass correct argument to xpath? There must be something about how to use single/double quotes. When I use variable

parser_xpath_identificator = "'//table/tbody[@id=\"threadbits_forum_251\"]/tr'" gives me an incorrect value or

parser_xpath_identificator = "'//table/tbody[@id="threadbits_forum_251"]/tr'" gives me an error syntax error, unexpected tIDENTIFIER, expecting $end

require 'rubygems'
require 'mechanize'

parser_xpath_identificator = "'//table/tbody[@id=\"threadbits_forum_251\"]/tr'"
#   parser_xpath_identificator = "'//table/tbody[@id="threadbits_forum_251"]/tr'"
    #gives an error: syntax error, unexpected tIDENTIFIER, expecting $end

agent = WWW::Mechanize.new
page = agent.get("http://www.vbulletin.org/forum/index.php")
page = page.link_with(:text=>'vB4 General Discussions').click
puts "Page title: #{page.title}"
puts "\nfrom variable: #{page.parser.xpath(parser_xpath_identificator).length}"
puts "directly: #{page.parser.xpath('//table/tbody[@id="threadbits_forum_251"]/tr').length}"
+2  A: 

In both cases you're nesting single-quotes directly inside double-quotes, which I don't think is correct. Try this:

parser_xpath_identificator = '//table/tbody[@id="threadbits_forum_251"]/tr'
Jordan
@Jordan: thank you for the correct answer and your comment. Now I can see that first " double quotes were not necessary at all ...
Radek