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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
local xml = require 'pl.xml'
local asserteq = require 'pl.test'.asserteq
local dump = require 'pl.pretty'.dump
-- Prosody stanza.lua style XML building
d = xml.new 'top' : addtag 'child' : text 'alice' : up() : addtag 'child' : text 'bob'
d = xml.new 'children' :
addtag 'child' :
addtag 'name' : text 'alice' : up() : addtag 'age' : text '5' : up() : addtag('toy',{type='fluffy'}) : up() :
up() :
addtag 'child':
addtag 'name' : text 'bob' : up() : addtag 'age' : text '6' : up() : addtag('toy',{type='squeaky'})
asserteq(
xml.tostring(d,'',' '),
[[
<children>
<child>
<name>alice</name>
<age>5</age>
<toy type='fluffy'/>
</child>
<child>
<name>bob</name>
<age>6</age>
<toy type='squeaky'/>
</child>
</children>]])
-- Orbit-style 'xmlification'
local children,child,toy,name,age = xml.tags 'children, child, toy, name, age'
d1 = children {
child {name 'alice', age '5', toy {type='fluffy'}},
child {name 'bob', age '6', toy {type='squeaky'}}
}
assert(xml.compare(d,d1))
-- or we can use a template document to convert Lua data to LOM
templ = child {name '$name', age '$age', toy{type='$toy'}}
d2 = children(templ:subst{
{name='alice',age='5',toy='fluffy'},
{name='bob',age='6',toy='squeaky'}
})
assert(xml.compare(d1,d2))
-- Parsing Google Weather service results --
local joburg = [[
<?xml version="1.0"?>
<xml_api_reply version='1'>
<weather module_id='0' tab_id='0' mobile_zipped='1' section='0' row='0' mobile_row='0'>
<forecast_information>
<city data='Johannesburg, Gauteng'/>
<postal_code data='Johannesburg,ZA'/>
<latitude_e6 data=''/>
<longitude_e6 data=''/>
<forecast_date data='2010-10-02'/>
<current_date_time data='2010-10-02 18:30:00 +0000'/>
<unit_system data='US'/>
</forecast_information>
<current_conditions>
<condition data='Clear'/>
<temp_f data='75'/>
<temp_c data='24'/>
<humidity data='Humidity: 19%'/>
<icon data='/ig/images/weather/sunny.gif'/>
<wind_condition data='Wind: NW at 7 mph'/>
</current_conditions>
<forecast_conditions>
<day_of_week data='Sat'/>
<low data='60'/>
<high data='89'/>
<icon data='/ig/images/weather/sunny.gif'/>
<condition data='Clear'/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data='Sun'/>
<low data='53'/>
<high data='86'/>
<icon data='/ig/images/weather/sunny.gif'/>
<condition data='Clear'/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data='Mon'/>
<low data='57'/>
<high data='87'/>
<icon data='/ig/images/weather/sunny.gif'/>
<condition data='Clear'/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data='Tue'/>
<low data='60'/>
<high data='84'/>
<icon data='/ig/images/weather/sunny.gif'/>
<condition data='Clear'/>
</forecast_conditions>
</weather>
</xml_api_reply>
]]
-- we particularly want to test the built-in XML parser here, not lxp.lom
local function parse (str)
return xml.parse(str,false,true)
end
local d = parse(joburg)
function match(t,xpect)
local res,ret = d:match(t)
asserteq(res,xpect,0,1) ---> note extra level, so we report on calls to this function!
end
t1 = [[
<weather>
<current_conditions>
<condition data='$condition'/>
<temp_c data='$temp'/>
</current_conditions>
</weather>
]]
match(t1,{
condition = "Clear",
temp = "24",
} )
t2 = [[
<weather>
{{<forecast_conditions>
<day_of_week data='$day'/>
<low data='$low'/>
<high data='$high'/>
<condition data='$condition'/>
</forecast_conditions>}}
</weather>
]]
local conditions = {
{
low = "60",
high = "89",
day = "Sat",
condition = "Clear",
},
{
low = "53",
high = "86",
day = "Sun",
condition = "Clear",
},
{
low = "57",
high = "87",
day = "Mon",
condition = "Clear",
},
{
low = "60",
high = "84",
day = "Tue",
condition = "Clear",
}
}
match(t2,conditions)
config = [[
<config>
<alpha>1.3</alpha>
<beta>10</beta>
<name>bozo</name>
</config>
]]
d,err = parse(config)
if not d then print(err); os.exit(1) end
-- can match against wildcard tag names (end with -)
-- can be names
match([[
<config>
{{<key->$value</key->}}
</config>
]],{
{key="alpha", value = "1.3"},
{key="beta", value = "10"},
{key="name",value = "bozo"},
})
-- can be numerical indices
match([[
<config>
{{<1->$2</1->}}
</config>
]],{
{"alpha","1.3"},
{"beta","10"},
{"name","bozo"},
})
-- _ is special; means 'this value is key of captured table'
match([[
<config>
{{<_->$1</_->}}
</config>
]],{
alpha = {"1.3"},
beta = {"10"},
name = {"bozo"},
})
-- the numerical index 0 is special: a capture of {[0]=val} becomes simply the value val
match([[
<config>
{{<_->$0</_->}}
</config>
]],{
alpha = "1.3",
name = "bozo",
beta = "10"
})
-- this can of course also work with attributes, but then we don't want to collapse!
config = [[
<config>
<alpha type='number'>1.3</alpha>
<beta type='number'>10</beta>
<name type='string'>bozo</name>
</config>
]]
d,err = parse(config)
if not d then print(err); os.exit(1) end
match([[
<config>
{{<_- type='$1'>$2</_->}}
</config>
]],{
alpha = {"number","1.3"},
beta = {"number","10"},
name = {"string","bozo"},
})
d,err = parse [[
<configuremap>
<configure name="NAME" value="ImageMagick"/>
<configure name="LIB_VERSION" value="0x651"/>
<configure name="LIB_VERSION_NUMBER" value="6,5,1,3"/>
<configure name="RELEASE_DATE" value="2009-05-01"/>
<configure name="VERSION" value="6.5.1"/>
<configure name="CC" value="vs7"/>
<configure name="HOST" value="windows-unknown-linux-gnu"/>
<configure name="DELEGATES" value="bzlib freetype jpeg jp2 lcms png tiff x11 xml wmf zlib"/>
<configure name="COPYRIGHT" value="Copyright (C) 1999-2009 ImageMagick Studio LLC"/>
<configure name="WEBSITE" value="http://www.imagemagick.org"/>
</configuremap>
]]
if not d then print(err); os.exit(1) end
--xml.debug = true
res,err = d:match [[
<configuremap>
{{<configure name="$_" value="$0"/>}}
</configuremap>
]]
asserteq(res,{
HOST = "windows-unknown-linux-gnu",
COPYRIGHT = "Copyright (C) 1999-2009 ImageMagick Studio LLC",
NAME = "ImageMagick",
LIB_VERSION = "0x651",
VERSION = "6.5.1",
RELEASE_DATE = "2009-05-01",
WEBSITE = "http://www.imagemagick.org",
LIB_VERSION_NUMBER = "6,5,1,3",
CC = "vs7",
DELEGATES = "bzlib freetype jpeg jp2 lcms png tiff x11 xml wmf zlib"
})
-- short excerpt from
-- /usr/share/mobile-broadband-provider-info/serviceproviders.xml
d = parse [[
<serviceproviders format="2.0">
<country code="za">
<provider>
<name>Cell-c</name>
<gsm>
<network-id mcc="655" mnc="07"/>
<apn value="internet">
<username>Cellcis</username>
<dns>196.7.0.138</dns>
<dns>196.7.142.132</dns>
</apn>
</gsm>
</provider>
<provider>
<name>MTN</name>
<gsm>
<network-id mcc="655" mnc="10"/>
<apn value="internet">
<dns>196.11.240.241</dns>
<dns>209.212.97.1</dns>
</apn>
</gsm>
</provider>
<provider>
<name>Vodacom</name>
<gsm>
<network-id mcc="655" mnc="01"/>
<apn value="internet">
<dns>196.207.40.165</dns>
<dns>196.43.46.190</dns>
</apn>
<apn value="unrestricted">
<name>Unrestricted</name>
<dns>196.207.32.69</dns>
<dns>196.43.45.190</dns>
</apn>
</gsm>
</provider>
<provider>
<name>Virgin Mobile</name>
<gsm>
<apn value="vdata">
<dns>196.7.0.138</dns>
<dns>196.7.142.132</dns>
</apn>
</gsm>
</provider>
</country>
</serviceproviders>
]]
res = d:match [[
<serviceproviders>
{{<country code="$_">
{{<provider>
<name>$0</name>
</provider>}}
</country>}}
</serviceproviders>
]]
asserteq(res,{
za = {
"Cell-c",
"MTN",
"Vodacom",
"Virgin Mobile"
}
})
res = d:match [[
<serviceproviders>
<country code="$country">
<provider>
<name>$name</name>
<gsm>
<apn value="$apn">
<dns>196.43.46.190</dns>
</apn>
</gsm>
</provider>
</country>
</serviceproviders>
]]
asserteq(res,{
name = "Vodacom",
country = "za",
apn = "internet"
})
d = parse[[
<!DOCTYPE xml>
<params>
<param>
<name>XXX</name>
<value></value>
</param>
<param>
<name>YYY</name>
<value>1</value>
</param>
</params>
]]
match([[
<params>
{{<param>
<name>$_</name>
<value>$0</value>
</param>}}
</params>
]],{XXX = '',YYY = '1'})
-- can always use xmlification to generate your templates...
local SP, country, provider, gsm, apn, dns = xml.tags 'serviceprovider, country, provider, gsm, apn, dns'
t = SP{country{code="$country",provider{
name '$name', gsm{apn {value="$apn",dns '196.43.46.190'}}
}}}
out = xml.tostring(t,' ',' ')
asserteq(out,[[
<serviceprovider>
<country code='$country'>
<provider>
<name>$name</name>
<gsm>
<apn value='$apn'>
<dns>196.43.46.190</dns>
</apn>
</gsm>
</provider>
</country>
</serviceprovider>]])
----- HTML is a degenerate form of XML ;)
-- attribute values don't need to be quoted, tags are case insensitive,
-- and some are treated as self-closing
doc = xml.parsehtml [[
<BODY a=1>
Hello dolly<br>
HTML is <b>slack</b><br>
</BODY>
]]
asserteq(xml.tostring(doc),[[
<body a='1'>
Hello dolly<br/>
HTML is <b>slack</b><br/></body>]])
doc = xml.parsehtml [[
<!DOCTYPE html>
<html lang=en>
<head><!--head man-->
</head>
<body>
</body>
</html>
]]
asserteq(xml.tostring(doc),"<html lang='en'><head/><body/></html>")
-- note that HTML mode currently barfs if there isn't whitespace around things
-- like '<' and '>' in scripts.
doc = xml.parsehtml [[
<html>
<head>
<script>function less(a,b) { return a < b; }</script>
</head>
<body>
<h2>hello dammit</h2>
</body>
</html>
]]
script = doc:get_elements_with_name 'script'
asserteq(script[1]:get_text(), 'function less(a,b) { return a < b; }')
-- test attribute order
local test_attrlist = xml.new('AttrList',{
Attr3="Value3",
['Attr1'] = "Value1",
['Attr2'] = "Value2",
[1] = 'Attr1', [2] = 'Attr2', [3] = 'Attr3'
})
asserteq(
xml.tostring(test_attrlist),
"<AttrList Attr1='Value1' Attr2='Value2' Attr3='Value3'/>"
)
-- commments
str = [[
<hello>
<!-- any <i>momentous</i> stuff here -->
dolly
</hello>
]]
doc = parse(str)
asserteq(xml.tostring(doc),[[
<hello>
dolly
</hello>]])
-- underscores and dashes in attributes
str = [[
<hello>
<tag my_attribute='my_value'>dolly</tag>
</hello>
]]
doc = parse(str)
print(doc)
print(xml.tostring(doc))
asserteq(xml.tostring(doc),[[
<hello><tag my_attribute='my_value'>dolly</tag></hello>]])
|