绝对可以以这种方式使用拉链,因为您可以在您选择的任何方向上移动拉链。作为一个例子,看看zip-visit 库,它提供了对 zippers 的任意访问,并能够根据需要更改节点。
从文档中获取的示例:
(def s "<div><span id='greeting'>Hello</span> <span id='name'>Mr. Foo</span>!</div>")
(def root (z/xml-zip (xml/parse (java.io.ByteArrayInputStream. (.getBytes s)))))
(defn replace-element [id replacement]
(visitor :pre [n s]
(if (= (:id (:attrs n)) id) {:node replacement})))
user=> (pprint (:node (visit root nil [(replace-element "name" "Mr. Smith")])))
{:tag :div,
:attrs nil,
:content
[{:tag :span, :attrs {:id "greeting"}, :content ["Hello"]}
"Mr. Smith"
"!"]}
当然,你也可以使用简单的步行来完成类似的任务,一个例子是关于遍历地图的这个 SO question。