2009-11-27 5 views
0
lotofxpath = arrayofmanyxpaths.map{|s| "\"" + s + "\""}.join(",") 
puts lotofxpath #=> "/html/body/a[1]", "/html/body/a[2]" 

newb = doc.xpath(lotofxpath).to_a 

cela ne fonctionnera pas, et se plaindre de xpath invalide.nokogiri: invalide xpath?

cependant, copier coller la chaîne de sortie

newb = doc.xpath("/html/body/a[1]", "/html/body/a[2]").to_a 

fonctionnera sans problème !!!

Que se passe-t-il ici?

Répondre

1

Dans le premier cas, vous finissez par appeler Nokogiri comme suit

newb = doc.xpath("\"/html/body/a[1]\", \"/html/body/a[2]\"").to_a 

et ce n'est pas la bonne syntaxe Ruby pour accomplir ce que vous essayez de faire. La bonne façon est

newb = doc.xpath(*arrayofmanyxpaths).to_a