如何在更复杂的HTML片段中使用Enlive中的选择器否定(但是......)?

Joc*_*Rau 15 html css clojure enlive

我有一个类似于的HTML代码段:

<div id="root">
    <div id="A" attrib_2="bar"></div>
    <div id="B" attrib_2="baz">
        <div id="H" attrib_1="gnu">
            <p>
                <div id="F" attrib_2="baz"></div>
            </p>
        </div>
    </div>
    <div id="C" attrib_2="owl"></div>
    <div id="D" attrib_2="uhu"></div>
    <div id="E" attrib_2="boom"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

现在,我想选择具有attrib_2(*[attrb_2])的所有片段,不包括具有attrib_1设置的节点的后代.使用任意标记可以有更多的嵌套级别(如<p>本例所示).有了Enlive(http://enlive.cgrand.net/),我已经尝试过类似的东西:

(select snippet [(but (attr? :attrib_1)) (attr? :attrib_2)])
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为否定(but (attr? :attrib_1))也与<p>标签相匹配.有没有办法用给定的选择器谓词(http://enlive.cgrand.net/syntax.html)来表达这个,或者我是否必须自己编写?

提前致谢

-Jochen

cgr*_*and 4

您必须编写自己的选择器:

(def parents 
  (zip-pred (fn [loc pred]
              (some pred (take-while identity (iterate zip/up loc))))))
Run Code Online (Sandbox Code Playgroud)

(未经测试)

进而

(select snippet [[(attr? :attrib_2) (but (parents (attr? :attrib_1))]])
Run Code Online (Sandbox Code Playgroud)

应该管用。