Max Pie
Max Pie

Reputation: 99

Nokogiri and Xpath query

I have this Xpath query inside a loop,

//div[@class='listing_content'][#{i}]/div/div/h3/a/text()

I want to process each node individually

The problem it gives the correct nodes, but all of them at once at once.

Also when i > 1, it returns nothing at all?

for i in (1...30)
  name = page.xpath("//div[@class='listing_content'][#{i}]/div/div/h3/a/text()")
  puts "this is name"
  puts name

  #Get Business phone
  phone = page.xpath("//div[@class='listing_content'][#{i}]//span[@class='business-phone phone']/text()")
  puts "this is phone"
  puts phone

  #Get Business website(if any)
  puts "this is website"
  website = page.xpath("//div[@class='listing_content'][#{i}]//li[@class='website-feature']//@href")
  puts website
end

Upvotes: 0

Views: 312

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

Also when i > 1, it returns nothing at all?

This is the second most FAQ in XPath:

Use:

(//div[@class='listing_content'])[#{i}]/div/div/h3/a/text()

The cause of the observed behavior is that in XPath the [] has higher precedence (priority) than the // pseudo-operator.

So, in your original expression you specify that every div[@class='listing_content'] element that is the i-th child of its parent should be selected.

However, in the XML document you are working with, every div[@class='listing_content'] happens to be the first (and only) child of its parent -- therefore if i > 1 then nothing is selected.

As in any other language, in order to override the default priority, we must use brackets.

Upvotes: 2

Related Questions