我有一个类似于的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
我是一个Rails开发人员在Clojure中弄湿我的脚.我正在尝试用ERB做一些非常简单的事情,但我无法在生活中把它弄清楚.
假设我在layout.html中有一个简单的网站布局文件:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我有这些片段,例如header.html和footer.html以及这条简单的路线.
(deftemplate layout "layout.html" [])
(defroutes home-routes
(GET "/" [] layout))
Run Code Online (Sandbox Code Playgroud)
如果请求转到"/",我怎样才能改变布局并将页眉和页脚片段插入其中?
I am trying use Clojure and Enlive to extract content of p
html tag under condition that one of attributes has values I designated. Something like this
<p itemprop="description"> Some content I want to extract </p>
Run Code Online (Sandbox Code Playgroud)
So I want to get Some content I want to extract
if itemprop="description"
.
I am very new to Clojure so help would be great.
为什么以下代码段不起作用?
(html/select (:body (client/post "http://www.web.onpe.gob.pe/modElecciones/elecciones/elecciones2011/2davuelta/onpe/presidente/extras/provincias.php" {:form-params {"elegido" "010000"}})) [:option])
Run Code Online (Sandbox Code Playgroud)
我是否必须先使用html-string将其转换为clojure数据结构或类似的东西?
I have recently inherited a non-finished web app written in Clojure, based on compojure and hiccup basically. It's a bad attempt to model some sort of MVC with OO style not in the FP style as seen here . So I bet to re-start the project almost from the scratch reusing the useful parts. I consider these alternatives:
The least breaking alternative would be Compojure+Enlive+jquery-pjax
Using a clojure web framework like Pedestal Any experiences about this?
The initial idea was …
我有一些看起来像这样的HTML:
<html>
<body>
<a href="www.google.com">text</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如何a
通过使用"查找带有文本内容的任何链接"的选择器来获取?我已经了解了text-pred
,但这只是返回文本,而不是带有文本的标签.
我需要从HTML页面的某个部分检索一些原始HTML.
我编写了刮刀并抓取了相应的div,但它返回了一个标签贴图.
(:use [net.cgrand.enlive-html :as html])
(defn fetch-url [url]
(html/html-resource (java.net.URL. url)))
(defn parse-test []
(let [url "http://www.ncbi.nlm.nih.gov/pubmedhealth/PMH0000928/"
url-data (fetch-url url)
id "a693025"]
(first (html/select url-data [(keyword (str "#" id "-why"))]))))
Run Code Online (Sandbox Code Playgroud)
这输出:
{:tag :div, :attrs {:class "section", :id "a693025-why"}, :content ({:tag :h2, :attrs nil, :content ({:tag :span, :attrs {:class "title"}, :content ("Why is this medication prescribed?")})} {:tag :p, :attrs nil, :content ("Zolpidem is used to treat insomnia (difficulty falling asleep or staying asleep). Zolpidem belongs to a class …
Run Code Online (Sandbox Code Playgroud) 请参阅此google群组主题
有些人表示混淆使用clone-for
将动态生成的元素列表插入到包含多个虚拟元素的模板中.例如,也许模板看起来像这样,
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我们想要像这样生成HTML,
<ul>
<li>real</li>
<li>data</li>
<li>here</li>
<li>wurdz</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
尝试天真的事情是这样的
(defsnippet my-snippet "my-template.html" [:ul] [items]
[[:li first-of-type]] (clone-for [ii items]
[:li] (content ii)))
Run Code Online (Sandbox Code Playgroud)
但是这会留下第二到第n个元素和虚拟数据.我们怎样才能简单地删除所有虚拟元素并替换为真实元素?
是否可以使用enlive将值附加到属性?
例如:我有这个
<a href="/item/edit/">edit</a>
Run Code Online (Sandbox Code Playgroud)
并希望这样
<a href="/item/edit/123">edit</a>
Run Code Online (Sandbox Code Playgroud)
我目前正在这样做:
(html/defsnippet foo "views/foo.html" [:#main]
[ctxt]
[:a] (html/set-attr :href (str "/item/edit/" (ctxt :id))))
Run Code Online (Sandbox Code Playgroud)
但我不希望将URL嵌入到我的代码中,只需将id附加到现有URL即可
(html/defsnippet foo "views/foo.html" [:#main]
[ctxt]
[:a@href] (html/append (ctxt :id)))
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用enlive的html-resource函数来抓取网页的内容,但我得到了响应403,因为我不是来自浏览器.我想这可以在Java中覆盖(在这里找到答案),但我会喜欢看一种处理这个问题的clojure方法.也许这可以通过为html-resource函数提供参数来实现,但我没有遇到过如何以及需要作为参数传递的示例.任何建议将不胜感激.
谢谢.
我有以下html加载到模板中...
Welcome <b id="alert-username">${fullname}</b>
Run Code Online (Sandbox Code Playgroud)
我在模板中有以下选择器/操作...
[:#alert-username] (replace-vars {:fullname (fullname request)})
Run Code Online (Sandbox Code Playgroud)
这呈现如下......
Welcome ${fullname}
Run Code Online (Sandbox Code Playgroud)
这显然不是我想要的.
但是,如果我这么做的话,它会起作用......
[:#alert-username] (fn [n] (assoc n :content [(fullname request)]))
Run Code Online (Sandbox Code Playgroud)
产量......
Welcome Bill
Run Code Online (Sandbox Code Playgroud)
所以我知道从请求中获取用户名并不是一个问题,因为上面的代码完成了它应该做的事情.
我在这做错了什么?
我不确定我应该如何接近这一点.我有一个CSS文件列表,我希望将其输入某些内容并获取HTML.例如,
(list "base.css" "index.css" "more_css.css") ;vector might be more appropriate?
Run Code Online (Sandbox Code Playgroud)
应转变为:
<link href="css/base.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<link href="css/more_css.css" rel="stylesheet" />
Run Code Online (Sandbox Code Playgroud)
从那里它应该被附加到<head>
.
defsnippet
几乎看起来合适,但为该文件的一部分采用模板文件和选择器.这里生成的HTML不依赖于模板,只生成HTML的东西似乎是合适的.clone-for
可能会做我想要的循环部分,但我无法弄清楚如何使用它.