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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
if not modules then modules = { } end modules ['lxml-xml'] = {
version = 1.001,
comment = "this module is the basis for the lxml-* ones",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local finalizers = xml.finalizers.xml
local xmlfilter = xml.filter -- we could inline this one for speed
local xmltostring = xml.tostring
local xmlserialize = xml.serialize
local function first(collected) -- wrong ?
return collected and collected[1]
end
local function last(collected)
return collected and collected[#collected]
end
local function all(collected)
return collected
end
local function reverse(collected)
if collected then
local reversed = { }
for c=#collected,1,-1 do
reversed[#reversed+1] = collected[c]
end
return reversed
end
end
local function attribute(collected,name)
if collected and #collected > 0 then
local at = collected[1].at
return at and at[name]
end
end
local function att(id,name)
local at = id.at
return at and at[name]
end
local function count(collected)
return (collected and #collected) or 0
end
local function position(collected,n)
if collected then
n = tonumber(n) or 0
if n < 0 then
return collected[#collected + n + 1]
elseif n > 0 then
return collected[n]
else
return collected[1].mi or 0
end
end
end
local function match(collected)
return (collected and collected[1].mi) or 0 -- match
end
local function index(collected)
if collected then
return collected[1].ni
end
end
local function attributes(collected,arguments)
if collected then
local at = collected[1].at
if arguments then
return at[arguments]
elseif next(at) then
return at -- all of them
end
end
end
local function chainattribute(collected,arguments) -- todo: optional levels
if collected then
local e = collected[1]
while e do
local at = e.at
if at then
local a = at[arguments]
if a then
return a
end
else
break -- error
end
e = e.__p__
end
end
return ""
end
local function raw(collected) -- hybrid
if collected then
local e = collected[1] or collected
return (e and xmlserialize(e)) or "" -- only first as we cannot concat function
else
return ""
end
end
local function text(collected) -- hybrid
if collected then
local e = collected[1] or collected
return (e and xmltostring(e.dt)) or ""
else
return ""
end
end
local function texts(collected)
if collected then
local t = { }
for c=1,#collected do
local e = collection[c]
if e and e.dt then
t[#t+1] = e.dt
end
end
return t
end
end
local function tag(collected,n)
if collected then
local c
if n == 0 or not n then
c = collected[1]
elseif n > 1 then
c = collected[n]
else
c = collected[#collected-n+1]
end
return c and c.tg
end
end
local function name(collected,n)
if collected then
local c
if n == 0 or not n then
c = collected[1]
elseif n > 1 then
c = collected[n]
else
c = collected[#collected-n+1]
end
if c then
if c.ns == "" then
return c.tg
else
return c.ns .. ":" .. c.tg
end
end
end
end
local function tags(collected,nonamespace)
if collected then
local t = { }
for c=1,#collected do
local e = collected[c]
local ns, tg = e.ns, e.tg
if nonamespace or ns == "" then
t[#t+1] = tg
else
t[#t+1] = ns .. ":" .. tg
end
end
return t
end
end
local function empty(collected)
if collected then
for c=1,#collected do
local e = collected[c]
if e then
local edt = e.dt
if edt then
local n = #edt
if n == 1 then
local edk = edt[1]
local typ = type(edk)
if typ == "table" then
return false
elseif edk ~= "" then -- maybe an extra tester for spacing only
return false
end
elseif n > 1 then
return false
end
end
end
end
end
return true
end
finalizers.first = first
finalizers.last = last
finalizers.all = all
finalizers.reverse = reverse
finalizers.elements = all
finalizers.default = all
finalizers.attribute = attribute
finalizers.att = att
finalizers.count = count
finalizers.position = position
finalizers.match = match
finalizers.index = index
finalizers.attributes = attributes
finalizers.chainattribute = chainattribute
finalizers.text = text
finalizers.texts = texts
finalizers.tag = tag
finalizers.name = name
finalizers.tags = tags
finalizers.empty = empty
-- shortcuts -- we could support xmlfilter(id,pattern,first)
function xml.first(id,pattern)
return first(xmlfilter(id,pattern))
end
function xml.last(id,pattern)
return last(xmlfilter(id,pattern))
end
function xml.count(id,pattern)
return count(xmlfilter(id,pattern))
end
function xml.attribute(id,pattern,a,default)
return attribute(xmlfilter(id,pattern),a,default)
end
function xml.raw(id,pattern)
if pattern then
return raw(xmlfilter(id,pattern))
else
return raw(id)
end
end
function xml.text(id,pattern)
if pattern then
-- return text(xmlfilter(id,pattern))
local collected = xmlfilter(id,pattern)
return (collected and xmltostring(collected[1].dt)) or ""
elseif id then
-- return text(id)
return xmltostring(id.dt) or ""
else
return ""
end
end
xml.content = text
function xml.position(id,pattern,n) -- element
return position(xmlfilter(id,pattern),n)
end
function xml.match(id,pattern) -- number
return match(xmlfilter(id,pattern))
end
function xml.empty(id,pattern)
return empty(xmlfilter(id,pattern))
end
xml.all = xml.filter
xml.index = xml.position
xml.found = xml.filter
|