1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
; Copyright (c) Chris Houser, April 2008. All rights reserved.
; The use and distribution terms for this software are covered by the
; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
; which can be found in the file CPL.TXT at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
; System for filtering trees and nodes generated by zip.clj in
; general, and xml trees in particular.
(in-ns 'zip-filter)
(clojure/refer 'clojure)
(defn coll?
"Returns true if x implements IPersistentCollection."
[x] (instance? clojure.lang.IPersistentCollection x))
(defn right-locs
"Returns a lazy sequence of locations to the right of loc."
[loc] (when loc (lazy-cons loc (right-locs (zip/right loc)))))
(defn leftmost?
"Returns true if there are no more nodes to the left of location loc."
[loc] (nil? (zip/left loc)))
(defn flatten
"Returns a lazy sequence of all descencents of location loc, in
depth-first order, left-to-right."
[loc]
(if (zip/branch? loc)
(lazy-cons loc (mapcat flatten (right-locs (zip/down loc))))
(list loc)))
(defn fixup-apply
"Calls (func loc), and then converts the result to the 'appropriate'
sequence."
#^{:private true}
[func loc]
(try
;(prn :PRE (zip/node loc))
(let [rtn (func loc)]
(cond (= rtn true) (list loc)
(contains? (meta rtn) :zip-filter/is-node?) (list rtn)
(= rtn false) nil
(= rtn nil) nil
(coll? rtn) rtn
:else (list rtn)))
(catch java.lang.NullPointerException e (prn :CAUGHT e))))
(defn seq-filter-expr
#^{:private true}
[func s] (mapcat (fn [loc] (fixup-apply func loc)) s))
(defn mapcat-chain
"Used in building query macros. See xml->"
#^{:private true}
[loc exprs func]
(let [prevseq (gensym 'prevseq_)]
`(let [initloc# ~loc
~prevseq
(list (with-meta initloc#
(assoc ^initloc# :zip-filter/is-node? true)))
~@(mapcat #(list prevseq
(list 'zip-filter/seq-filter-expr
(let [usercode (func %)]
(cond usercode usercode
:else %))
prevseq))
exprs)]
~prevseq)))
; === xml-zipper query specialization ===
(in-ns 'zip-filter-xml)
(clojure/refer 'clojure)
(refer 'zip-filter)
(defn attr
"Returns the xml attribute named attrname, of the xml node at location loc."
[loc attrname]
(let [n (zip/node loc) a (n :attrs)]
(and a (a attrname))))
(defn attr=
"Returns a query predicate that matches a node when it has an
attribute named attrname whose value is attrval."
[attrname attrval]
(fn [loc] (= (attr loc attrname) attrval)))
(defn tag=
"Returns a query predicate that matches a node when its is a tag
named tagname."
[tagname]
(fn [loc]
(if (zip/branch? loc)
(filter #(and (zip/branch? %) (= tagname ((zip/node %) :tag)))
(right-locs (zip/down loc)))
nil)))
(defn content
"Returns the contents of the xml node at location loc."
[loc] ((zip/node loc) :content))
(defn content=
"Returns a query predicate that matches a node when its xml content equals s."
[s] (fn [loc] (= ((zip/node loc) :content) [s])))
(defmacro seq-test
"Returns a query predicate that matches a node when its xml content
matches the query expresions given."
#^{:private true}
[& preds] `(fn [loc#] (and (xml-> loc# ~@preds) (list loc#))))
(defmacro xml->
"The loc is passed to the first predicate. If the predicate returns
a collection, each value of the collection is passed to the next
predicate. If it returns a location, the location is passed to the
next predicate. If it returns true, the input location is passed to
the next predicate. If it returns false or nil, the next predicate
is not called.
This process is repeated, passing the processed results of each
predicate to the next predicate. xml-> returns the final sequence.
The entire chain is evaluated lazily.
There are also special predicates: keywords are converted to
xml-tag=, strings to xml-content=, and vectors to sub-queries that
return true if they match.
See the footer of zip-query.clj for examples."
[loc & preds]
(mapcat-chain loc preds
#(cond (keyword? %) (list 'tag= %)
(string? %) (list 'content= %)
(vector? %) (list* 'seq-test %))))
(defmacro xml1->
"Returns the first item from loc based on the query predicates
given. See xml->"
[loc & preds] `(first (xml-> ~loc ~@preds)))
(defn clean-str
"Returns the textual contents of the given sequence of xml
locations, similar to xpaths's value-of"
[locseq]
(. #^String (apply str (mapcat #(xml-> % flatten zip/node string?) locseq))
(replaceAll (str "[\\s" (char 160) "]+") " ")))
; === examples ===
(comment
(defn parse-str [s]
(zip/xml-zip (xml/parse (new org.xml.sax.InputSource
(new java.io.StringReader s)))))
(def atom1 (parse-str "<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'>
<id>tag:blogger.com,1999:blog-28403206</id>
<updated>2008-02-14T08:00:58.567-08:00</updated>
<title type='text'>n01senet</title>
<link rel='alternate' type='text/html' href='http://n01senet.blogspot.com/'/>
<entry>
<id>1</id>
<published>2008-02-13</published>
<title type='text'>clojure is the best lisp yet</title>
<author><name>Chouser</name></author>
</entry>
<entry>
<id>2</id>
<published>2008-02-07</published>
<title type='text'>experimenting with vnc</title>
<author><name>agriffis</name></author>
</entry>
</feed>
"))
; simple single-function filter
(assert (= (xml-> atom1 #((zip/node %) :tag))
'(:feed)))
; two-stage filter using helpful query prediates
(assert (= (xml-> atom1 (tag= :title) content)
'("n01senet")))
; same filter as above, this time using keyword shortcut
(assert (= (xml-> atom1 :title content)
'("n01senet")))
; multi-stage filter
(assert (= (xml-> atom1 :entry :author :name content)
'("Chouser" "agriffis")))
; multi-stage filter with subquery specified using a vector
(assert (= (xml-> atom1 :entry [:author :name (content= "agriffis")]
:id content)
'("2")))
; same filter as above, this time using a string shortcut
(assert (= (xml-> atom1 :entry [:author :name "agriffis"] :id content)
'("2")))
; attribute access
(assert (= (xml-> atom1 :title #(attr % :type))
'("text")))
; attribute filtering
(assert (= (xml-> atom1 :link [(attr= :rel "alternate")] #(attr % :type))
'("text/html")))
|