Search code examples
clojure

Using the hickory library, is it possible to use selectors in combination with zippers?


I'm new to Clojure, and hickory, and the idea of zippers.

What I want to do is, I want to use selectors to go to one location in an HTML document. And then, I want to be able to navigate from that location, up to a parent element, and then get 2nd sibling from that point.

Is this possible to do with hickory? From what I understand, it seems as though I only have the option of using selectors, or navigating the HTML in a zipper structure, but I can't figure out how to do both, or if that's even possible.


Solution

  • You could do something like this:

        (:require
            [hickory.select :as s]
            [hickory.convert :as convert]
            [clojure.zip :as z]
    ...
    
        (let [html (convert/hiccup-to-hickory (list [:div
                                                     [:div {:class "didya"} "nevertheless"]]
                                                    [:div "possible"]
                                                    [:div "geometric"]))]
    
            (-> (s/select-locs (s/class "didya") html)
                (first)
                (z/up)
                (z/right)
                (z/right)
                (z/node)))