我正在走一个 html/xml 数据结构。我使用clojure.zip
. 一旦找到我想要cut
(修剪)的节点,我就找不到删除所有子节点和右节点的方法。
例子:
假设我有这棵树(代表 html):
(def tree [:p "F"
[:p "G" [:p "I" [:p "H"]]]
[:p "B"
[:p
"D"
[:p "E"]
[:p "C"]]
[:p "A"]]])
我解析它,xml-zip
它,在行走时的某个时刻,我最终到达了节点“D”,我想在其中剪切。我现在需要返回没有“E”、“C”(子)和“D”的根。这些是此时使用时尚未访问的所有节点next
。
我将如何删除这些节点?
注意:如果这不可行,我也欢迎一种复制拉链的方法cut
。
示例数据:这是我对上述树的解析数据,我称之为xml-zip
:
{:tag :html, :attrs nil, :content [{:tag :head, :attrs nil, :content nil} {:tag :body, :attrs nil, :content [{:tag :p, :attrs nil, :content ["F"]} {:tag :p, :attrs nil, :content ["G"]} {:tag :p, :attrs nil, :content ["I"]} {:tag :p, :attrs nil, :content ["H"]} {:tag :p, :attrs nil, :content nil} {:tag :p, :attrs nil, :content nil} {:tag :p, :attrs nil, :content ["B"]} {:tag :p, :attrs nil, :content ["D"]} {:tag :p, :attrs nil, :content ["E"]} {:tag :p, :attrs nil, :content ["C"]} {:tag :p, :attrs nil, :content nil} {:tag :p, :attrs nil, :content ["A"]} {:tag :p, :attrs nil, :content nil} {:tag :p, :attrs nil, :content nil}]}]}
我开始像这样遍历它以获取内容:
(-> parsed (z/xml-zip)
(z/down) ;head
(z/right) ; body
(z/down) ; content
)
另一个例子:
以下字符串:"<article><h1><img href=\"some-url\"></img> some-text <b>in bold</b></h1><ul><li> AA </li> <li>BB</li></ul></article>"
将为我提供以下地图:
[{:tag :html, :attrs nil, :content [{:tag :head, :attrs nil, :content nil} {:tag :body, :attrs nil, :content [{:tag :article, :attrs nil, :content [{:tag :h1, :attrs nil, :content [{:tag :img, :attrs {:href "some-url"}, :content nil} " some-text " {:tag :b, :attrs nil, :content ["in bold"]}]} {:tag :ul, :attrs nil, :content [{:tag :li, :attrs nil, :content [" AA "]} " " {:tag :li, :attrs nil, :content ["BB"]}]}]}]}]} nil]
在“some-text”切割时,它最终应该导致字符串 <article><h1><img href=\"some-url\"></img> some-text</h1></article>