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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
|
# -*- coding: utf-8 -*-
"""
Test cases related to ISO-Schematron parsing and validation
"""
from __future__ import absolute_import
import unittest
from lxml import isoschematron
from .common_imports import etree, HelperTestCase, fileInTestDir, doctest, make_doctest
class ETreeISOSchematronTestCase(HelperTestCase):
def test_schematron(self):
tree_valid = self.parse('<AAA><BBB/><CCC/></AAA>')
tree_invalid = self.parse('<AAA><BBB/><CCC/><DDD/></AAA>')
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern id="OpenModel">
<title>Open Model</title>
<rule context="AAA">
<assert test="BBB"> BBB element is not present</assert>
<assert test="CCC"> CCC element is not present</assert>
</rule>
</pattern>
<pattern id="ClosedModel">
<title>Closed model"</title>
<rule context="AAA">
<assert test="BBB"> BBB element is not present</assert>
<assert test="CCC"> CCC element is not present</assert>
<assert test="count(BBB|CCC) = count (*)">There is an extra element</assert>
</rule>
</pattern>
</schema>
''')
schema = isoschematron.Schematron(schema)
self.assertTrue(schema.validate(tree_valid))
self.assertTrue(not schema.validate(tree_invalid))
def test_schematron_elementtree_error(self):
self.assertRaises(ValueError, isoschematron.Schematron, etree.ElementTree())
# an empty pattern is valid in iso schematron
def test_schematron_empty_pattern(self):
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern id="OpenModel">
<title>Open model</title>
</pattern>
</schema>
''')
schema = isoschematron.Schematron(schema)
self.assertTrue(schema)
def test_schematron_invalid_schema_empty(self):
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" />
''')
self.assertRaises(etree.SchematronParseError,
isoschematron.Schematron, schema)
def test_schematron_invalid_schema_namespace(self):
schema = self.parse('''\
<schema xmlns="mynamespace" />
''')
self.assertRaises(etree.SchematronParseError,
isoschematron.Schematron, schema)
def test_schematron_from_tree(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(isinstance(schematron, isoschematron.Schematron))
def test_schematron_from_element(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
schematron = isoschematron.Schematron(schema.getroot())
self.assertTrue(isinstance(schematron, isoschematron.Schematron))
def test_schematron_from_file(self):
schematron = isoschematron.Schematron(file=fileInTestDir('test.sch'))
self.assertTrue(isinstance(schematron, isoschematron.Schematron))
def test_schematron_call(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron(tree_valid), schematron.error_log)
valid = schematron(tree_invalid)
self.assertTrue(not valid)
def test_schematron_validate(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron.validate(tree_valid), schematron.error_log)
valid = schematron.validate(tree_invalid)
self.assertTrue(not valid)
def test_schematron_assertValid(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron(tree_valid), schematron.error_log)
self.assertRaises(etree.DocumentInvalid, schematron.assertValid,
tree_invalid)
def test_schematron_error_log(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron(tree_valid), schematron.error_log)
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(len(schematron.error_log), 1,
'expected single error: %s (%s errors)' %
(schematron.error_log, len(schematron.error_log)))
def test_schematron_result_report(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
schematron = isoschematron.Schematron(schema, store_report=True)
self.assertTrue(schematron(tree_valid), schematron.error_log)
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertTrue(
isinstance(schematron.validation_report, etree._ElementTree),
'expected a validation report result tree, got: %s' % schematron.validation_report)
schematron = isoschematron.Schematron(schema, store_report=False)
self.assertTrue(schematron(tree_valid), schematron.error_log)
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertTrue(schematron.validation_report is None,
'validation reporting switched off, still: %s' % schematron.validation_report)
def test_schematron_store_schematron(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron.validator_xslt is None)
schematron = isoschematron.Schematron(schema, store_schematron=True)
self.assertTrue(isinstance(schematron.schematron, etree._ElementTree),
'expected schematron schema to be stored')
def test_schematron_store_xslt(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron.validator_xslt is None)
schematron = isoschematron.Schematron(schema, store_xslt=True)
self.assertTrue(isinstance(schematron.validator_xslt, etree._ElementTree),
'expected validator xslt to be stored')
def test_schematron_abstract(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:title>iso schematron validation</sch:title>
<sch:ns uri="http://www.w3.org/2001/XMLSchema-instance" prefix="xsi"/>
<sch:ns uri="http://codespeak.net/lxml/objectify/pytype" prefix="py"/>
<!-- of course, these only really make sense when combined with a schema that
ensures datatype xs:dateTime -->
<sch:pattern abstract="true" id="abstract.dateTime.tz_utc">
<sch:rule context="$datetime">
<sch:let name="tz" value="concat(substring-after(substring-after(./text(), 'T'), '+'), substring-after(substring-after(./text(), 'T'), '-'))"/>
<sch:let name="lastchar" value="substring(./text(), string-length(./text()))"/>
<sch:assert test="$lastchar='Z' or $tz='00:00'">[ERROR] element (<sch:value-of select="name(.)"/>) dateTime value (<sch:value-of select="."/>) is not qualified as UTC (tz: <sch:value-of select="$tz"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern abstract="true" id="abstract.dateTime.tz_utc_nillable">
<sch:rule context="$datetime">
<sch:let name="tz" value="concat(substring-after(substring-after(./text(), 'T'), '+'), substring-after(substring-after(./text(), 'T'), '-'))"/>
<sch:let name="lastchar" value="substring(./text(), string-length(./text()))"/>
<sch:assert test="@xsi:nil='true' or ($lastchar='Z' or $tz='00:00')">[ERROR] element (<sch:value-of select="name(.)"/>) dateTime value (<sch:value-of select="."/>) is not qualified as UTC (tz: <sch:value-of select="$tz"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern is-a="abstract.dateTime.tz_utc" id="datetime" >
<sch:param name="datetime" value="datetime"/>
</sch:pattern>
<sch:pattern is-a="abstract.dateTime.tz_utc_nillable" id="nillableDatetime">
<sch:param name="datetime" value="nillableDatetime"/>
</sch:pattern>
</sch:schema>
''')
valid_trees = [
self.parse('''\
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime>2009-12-10T15:21:00Z</datetime>
<nillableDatetime xsi:nil="true"/>
</root>
'''),
self.parse('''\
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime>2009-12-10T15:21:00Z</datetime>
<nillableDatetime>2009-12-10T15:21:00Z</nillableDatetime>
</root>
'''),
self.parse('''\
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime>2009-12-10T15:21:00+00:00</datetime>
<nillableDatetime>2009-12-10T15:21:00-00:00</nillableDatetime>
</root>
'''),
]
schematron = isoschematron.Schematron(schema)
for tree_valid in valid_trees:
self.assertTrue(schematron(tree_valid), schematron.error_log)
tree_invalid = self.parse('''\
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime>2009-12-10T16:21:00+01:00</datetime>
<nillableDatetime>2009-12-10T16:21:00+01:00</nillableDatetime>
</root>
''')
expected = 2
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
tree_invalid = self.parse('''\
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime xsi:nil="true"/>
<nillableDatetime>2009-12-10T16:21:00Z</nillableDatetime>
</root>
''')
expected = 1
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
def test_schematron_phases(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:title>iso schematron validation</sch:title>
<sch:ns uri="http://www.w3.org/2001/XMLSchema-instance" prefix="xsi"/>
<sch:ns uri="http://codespeak.net/lxml/objectify/pytype" prefix="py"/>
<sch:phase id="mandatory">
<sch:active pattern="number_of_entries"/>
</sch:phase>
<sch:phase id="datetime_checks">
<sch:active pattern="datetime"/>
<sch:active pattern="nillableDatetime"/>
</sch:phase>
<sch:phase id="full">
<sch:active pattern="number_of_entries"/>
<sch:active pattern="datetime"/>
<sch:active pattern="nillableDatetime"/>
</sch:phase>
<!-- of course, these only really make sense when combined with a schema that
ensures datatype xs:dateTime -->
<sch:pattern abstract="true" id="abstract.dateTime.tz_utc">
<sch:rule context="$datetime">
<sch:let name="tz" value="concat(substring-after(substring-after(./text(), 'T'), '+'), substring-after(substring-after(./text(), 'T'), '-'))"/>
<sch:let name="lastchar" value="substring(./text(), string-length(./text()))"/>
<sch:assert test="$lastchar='Z' or $tz='00:00'">[ERROR] element (<sch:value-of select="name(.)"/>) dateTime value (<sch:value-of select="."/>) is not qualified as UTC (tz: <sch:value-of select="$tz"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern abstract="true" id="abstract.dateTime.tz_utc_nillable">
<sch:rule context="$datetime">
<sch:let name="tz" value="concat(substring-after(substring-after(./text(), 'T'), '+'), substring-after(substring-after(./text(), 'T'), '-'))"/>
<sch:let name="lastchar" value="substring(./text(), string-length(./text()))"/>
<sch:assert test="@xsi:nil='true' or ($lastchar='Z' or $tz='00:00')">[ERROR] element (<sch:value-of select="name(.)"/>) dateTime value (<sch:value-of select="."/>) is not qualified as UTC (tz: <sch:value-of select="$tz"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries test</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern id="datetime" is-a="abstract.dateTime.tz_utc">
<sch:param name="datetime" value="datetime"/>
</sch:pattern>
<sch:pattern id="nillableDatetime" is-a="abstract.dateTime.tz_utc_nillable">
<sch:param name="datetime" value="nillableDatetime"/>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime>2009-12-10T15:21:00Z</datetime>
<nillableDatetime xsi:nil="true"/>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<datetime>2009-12-10T16:21:00+01:00</datetime>
<nillableDatetime>2009-12-10T16:21:00+01:00</nillableDatetime>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
# check everything (default phase #ALL)
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 3
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
# check phase mandatory
schematron = isoschematron.Schematron(
schema, compile_params={'phase': 'mandatory'})
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 1
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
# check phase datetime_checks
schematron = isoschematron.Schematron(
schema, compile_params={'phase': 'datetime_checks'})
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 2
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
# check phase full
schematron = isoschematron.Schematron(
schema, compile_params={'phase': 'full'})
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 3
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
def test_schematron_phases_kwarg(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:title>iso schematron validation</sch:title>
<sch:ns uri="http://www.w3.org/2001/XMLSchema-instance" prefix="xsi"/>
<sch:ns uri="http://codespeak.net/lxml/objectify/pytype" prefix="py"/>
<sch:phase id="mandatory">
<sch:active pattern="number_of_entries"/>
</sch:phase>
<sch:phase id="datetime_checks">
<sch:active pattern="datetime"/>
<sch:active pattern="nillableDatetime"/>
</sch:phase>
<sch:phase id="full">
<sch:active pattern="number_of_entries"/>
<sch:active pattern="datetime"/>
<sch:active pattern="nillableDatetime"/>
</sch:phase>
<!-- of course, these only really make sense when combined with a schema that
ensures datatype xs:dateTime -->
<sch:pattern abstract="true" id="abstract.dateTime.tz_utc">
<sch:rule context="$datetime">
<sch:let name="tz" value="concat(substring-after(substring-after(./text(), 'T'), '+'), substring-after(substring-after(./text(), 'T'), '-'))"/>
<sch:let name="lastchar" value="substring(./text(), string-length(./text()))"/>
<sch:assert test="$lastchar='Z' or $tz='00:00'">[ERROR] element (<sch:value-of select="name(.)"/>) dateTime value (<sch:value-of select="."/>) is not qualified as UTC (tz: <sch:value-of select="$tz"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern abstract="true" id="abstract.dateTime.tz_utc_nillable">
<sch:rule context="$datetime">
<sch:let name="tz" value="concat(substring-after(substring-after(./text(), 'T'), '+'), substring-after(substring-after(./text(), 'T'), '-'))"/>
<sch:let name="lastchar" value="substring(./text(), string-length(./text()))"/>
<sch:assert test="@xsi:nil='true' or ($lastchar='Z' or $tz='00:00')">[ERROR] element (<sch:value-of select="name(.)"/>) dateTime value (<sch:value-of select="."/>) is not qualified as UTC (tz: <sch:value-of select="$tz"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries test</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern id="datetime" is-a="abstract.dateTime.tz_utc">
<sch:param name="datetime" value="datetime"/>
</sch:pattern>
<sch:pattern id="nillableDatetime" is-a="abstract.dateTime.tz_utc_nillable">
<sch:param name="datetime" value="nillableDatetime"/>
</sch:pattern>
</sch:schema>
''')
tree_valid = self.parse('''\
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<datetime>2009-12-10T15:21:00Z</datetime>
<nillableDatetime xsi:nil="true"/>
<number_of_entries>0</number_of_entries>
<entries>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<datetime>2009-12-10T16:21:00+01:00</datetime>
<nillableDatetime>2009-12-10T16:21:00+01:00</nillableDatetime>
<number_of_entries>3</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
# check everything (default phase #ALL)
schematron = isoschematron.Schematron(schema)
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 3
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
# check phase mandatory
schematron = isoschematron.Schematron(schema, phase='mandatory')
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 1
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
# check phase datetime_checks
schematron = isoschematron.Schematron(schema, phase='datetime_checks')
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 2
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected,
'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
# check phase full
schematron = isoschematron.Schematron(schema, phase='full')
self.assertTrue(schematron(tree_valid), schematron.error_log)
expected = 3
valid = schematron(tree_invalid)
self.assertTrue(not valid)
self.assertEqual(
len(schematron.error_log), expected, 'expected %s errors: %s (%s errors)' %
(expected, schematron.error_log, len(schematron.error_log)))
def test_schematron_xmlschema_embedded(self):
schema = self.parse('''\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<xs:element name="message">
<xs:complexType>
<xs:sequence>
<xs:element name="number_of_entries" type="xs:positiveInteger">
<xs:annotation>
<xs:appinfo>
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="entries">
<xs:complexType>
<xs:sequence>
<xs:element name="entry" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>2</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>1</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
xmlschema = etree.XMLSchema(schema)
schematron = isoschematron.Schematron(schema)
# fwiw, this must also be XMLSchema-valid
self.assertTrue(xmlschema(tree_valid), xmlschema.error_log)
self.assertTrue(schematron(tree_valid))
# still schema-valid
self.assertTrue(xmlschema(tree_invalid), xmlschema.error_log)
self.assertTrue(not schematron(tree_invalid))
def test_schematron_relaxng_embedded(self):
schema = self.parse('''\
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
xmlns:sch="http://purl.oclc.org/dsdl/schematron"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<ref name="message"/>
</start>
<define name="message">
<element name="message">
<element name="number_of_entries">
<!-- RelaxNG can be mixed freely with stuff from other namespaces -->
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
<data type="positiveInteger"/>
</element>
<element name="entries">
<zeroOrMore>
<element name="entry"><data type="string"/></element>
</zeroOrMore>
</element>
</element>
</define>
</grammar>
''')
tree_valid = self.parse('''\
<message>
<number_of_entries>2</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
tree_invalid = self.parse('''\
<message>
<number_of_entries>1</number_of_entries>
<entries>
<entry>Entry 1</entry>
<entry>Entry 2</entry>
</entries>
</message>
''')
relaxng = etree.RelaxNG(schema)
schematron = isoschematron.Schematron(schema)
# fwiw, this must also be RelaxNG-valid
self.assertTrue(relaxng(tree_valid), relaxng.error_log)
self.assertTrue(schematron(tree_valid))
# still schema-valid
self.assertTrue(relaxng(tree_invalid), relaxng.error_log)
self.assertTrue(not schematron(tree_invalid))
def test_schematron_invalid_args(self):
schema = self.parse('''\
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:pattern id="number_of_entries">
<sch:title>mandatory number_of_entries tests</sch:title>
<sch:rule context="number_of_entries">
<sch:assert test="text()=count(../entries/entry)">[ERROR] number_of_entries (<sch:value-of select="."/>) must equal the number of entries/entry elements (<sch:value-of select="count(../entries/entry)"/>)</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
''')
# handing phase as keyword arg will *not* raise the type error
self.assertRaises(TypeError, isoschematron.Schematron, schema,
compile_params={'phase': None})
def test_schematron_customization(self):
class MySchematron(isoschematron.Schematron):
def _extract(self, root):
schematron = (root.xpath(
'//sch:schema',
namespaces={'sch': "http://purl.oclc.org/dsdl/schematron"})
or [None])[0]
return schematron
def _include(self, schematron, **kwargs):
raise RuntimeError('inclusion unsupported')
def _expand(self, schematron, **kwargs):
raise RuntimeError('expansion unsupported')
def _validation_errors(self, validationReport):
valid = etree.XPath(
'count(//svrl:successful-report[@flag="critical"])=1',
namespaces={'svrl': isoschematron.SVRL_NS})(
validationReport)
if valid:
return []
error = etree.Element('Error')
error.text = 'missing critical condition report'
return [error]
tree_valid = self.parse('<AAA><BBB/><CCC/></AAA>')
tree_invalid = self.parse('<AAA><BBB/><CCC/><DDD/></AAA>')
schema = self.parse('''\
<schema xmlns="http://www.example.org/yet/another/schema/dialect">
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern id="OpenModel">
<title>Open Model</title>
<rule context="AAA">
<report test="BBB" flag="info">BBB element must be present</report>
<report test="CCC" flag="info">CCC element must be present</report>
</rule>
</pattern>
<pattern id="ClosedModel">
<title>Closed model"</title>
<rule context="AAA">
<report test="BBB" flag="info">BBB element must be present</report>
<report test="CCC" flag="info">CCC element must be present</report>
<report test="count(BBB|CCC) = count(*)" flag="critical">Only BBB and CCC children must be present</report>
</rule>
</pattern>
</schema>
</schema>
''')
# check if overridden _include is run
self.assertRaises(RuntimeError, MySchematron, schema, store_report=True)
# check if overridden _expand is run
self.assertRaises(RuntimeError, MySchematron, schema, store_report=True,
include=False)
schema = MySchematron(schema, store_report=True, include=False,
expand=False)
self.assertTrue(schema.validate(tree_valid))
self.assertTrue(not schema.validate(tree_invalid))
#TODO: test xslt parameters for inclusion, expand & compile steps (?)
def test_schematron_fail_on_report(self):
tree_valid = self.parse('<AAA><BBB/><CCC/></AAA>')
tree_invalid = self.parse('<AAA><BBB/><CCC/><DDD/></AAA>')
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern id="OpenModel">
<title>Simple Report</title>
<rule context="AAA">
<report test="DDD"> DDD element must not be present</report>
</rule>
</pattern>
</schema>
''')
schema_report = isoschematron.Schematron(
schema, error_finder=isoschematron.Schematron.ASSERTS_AND_REPORTS)
schema_no_report = isoschematron.Schematron(schema)
self.assertTrue(schema_report.validate(tree_valid))
self.assertTrue(not schema_report.validate(tree_invalid))
self.assertTrue(schema_no_report.validate(tree_valid))
self.assertTrue(schema_no_report.validate(tree_invalid))
def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeISOSchematronTestCase)])
suite.addTests(doctest.DocTestSuite(isoschematron))
suite.addTests(
[make_doctest('../../../doc/validation.txt')])
return suite
if __name__ == '__main__':
print('to test use test.py %s' % __file__)
|