Search code examples
rubyhpricot

Multiple search with Hpricot


Had RTFM'ed, but still puzzled. I need to get objects which satisfy at least one of the property condition list.

E.g. divs, where class == "marked" OR class = "data" OR class = "comments"

For now emulated it manually, but is it possible with Hpricot standard abilities?


Solution

  • doc = Hpricot.parse(..your data...)
    divs = doc.search("//div[@class='marked' or @class='data' or @class='comments']")
    

    The search takes an xpath expression, and xpath allows logical and and or operators. See this great answer about a similar question: XPATH Multiple Element Filters.