lxml.tests.test_relaxng
1
2
3 """
4 Test cases related to RelaxNG parsing and validation
5 """
6
7 import unittest , sys , os . path
8
9 this_dir = os . path . dirname ( __file__ )
10 if this_dir not in sys . path :
11 sys . path . insert ( 0 , this_dir )
12
13 from common_imports import etree , BytesIO , _bytes , HelperTestCase , fileInTestDir
14 from common_imports import doctest , make_doctest , skipif
15
16 try :
17 import rnc2rng
18 except ImportError :
19 rnc2rng = None
20
21
44
46 tree_valid = self . parse ( '<a><b></b></a>' )
47 tree_invalid = self . parse ( '<a><c></c></a>' )
48 schema_file = BytesIO ( '''\
49 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
50 <zeroOrMore>
51 <element name="b">
52 <text />
53 </element>
54 </zeroOrMore>
55 </element>
56 ''' )
57 schema = etree . RelaxNG ( file = schema_file )
58 self . assertTrue ( schema . validate ( tree_valid ) )
59 self . assertFalse ( schema . validate ( tree_invalid ) )
60
63
65 tree_invalid = self . parse ( '<a><c></c></a>' )
66 schema = self . parse ( '''\
67 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
68 <zeroOrMore>
69 <element name="b">
70 <text />
71 </element>
72 </zeroOrMore>
73 </element>
74 ''' )
75 schema = etree . RelaxNG ( schema )
76 self . assertFalse ( schema . validate ( tree_invalid ) )
77 errors = schema . error_log
78 self . assertTrue ( [ log for log in errors
79 if log . level_name == "ERROR" ] )
80 self . assertTrue ( [ log for log in errors
81 if "not expect" in log . message ] )
82
84 tree_invalid = self . parse ( '''\
85 <test>
86 <reference id="my-ref">This is my unique ref.</reference>
87 <data ref="my-ref">Valid data</data>
88 <data ref="myref">Invalid data</data>
89 </test>
90 ''' )
91 schema = self . parse ( '''\
92 <grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
93 xmlns="http://relaxng.org/ns/structure/1.0">
94 <define name="by-ref">
95 <data type="IDREF"/>
96 </define>
97 <start>
98 <element name="test">
99 <zeroOrMore>
100 <element name="reference">
101 <attribute name="id">
102 <data type="ID"/>
103 </attribute>
104 <text/>
105 </element>
106 </zeroOrMore>
107 <zeroOrMore>
108 <element name="data">
109 <attribute name="ref">
110 <data type="IDREF"/>
111 </attribute>
112 <text/>
113 </element>
114 </zeroOrMore>
115 </element>
116 </start>
117 </grammar>
118 ''' )
119
120 schema = etree . RelaxNG ( schema )
121 self . assertFalse ( schema . validate ( tree_invalid ) )
122 errors = schema . error_log
123 self . assertTrue ( errors )
124 self . assertTrue ( [ log for log in errors if "IDREF" in log . message ] )
125 self . assertTrue ( [ log for log in errors if "myref" in log . message ] )
126
128 schema = self . parse ( '''\
129 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
130 <zeroOrMore>
131 <element name="b" />
132 </zeroOrMore>
133 </element>
134 ''' )
135 self . assertRaises ( etree . RelaxNGParseError ,
136 etree . RelaxNG , schema )
137
144
146 schema = self . parse ( '''\
147 <grammar xmlns="http://relaxng.org/ns/structure/1.0">
148 <define name="test">
149 <element name="test"/>
150 </define>
151 </grammar>
152 ''' )
153 self . assertRaises ( etree . RelaxNGParseError ,
154 etree . RelaxNG , schema )
155
163
172
174 tree_valid = self . parse ( '<a><b></b></a>' )
175 tree_invalid = self . parse ( '<a><c></c></a>' )
176 schema = self . parse ( '''\
177 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
178 <zeroOrMore>
179 <element name="b">
180 <text />
181 </element>
182 </zeroOrMore>
183 </element>
184 ''' )
185 self . assertTrue ( tree_valid . relaxng ( schema ) )
186 self . assertFalse ( tree_invalid . relaxng ( schema ) )
187
220
244
245
254
255 if __name__ == '__main__' :
256 print ( 'to test use test.py %s' % __file__ )
257