本文整理汇总了Python中xmlschema.XMLSchema方法的典型用法代码示例。如果您正苦于以下问题:Python xmlschema.XMLSchema方法的具体用法?Python xmlschema.XMLSchema怎么用?Python xmlschema.XMLSchema使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xmlschema
的用法示例。
在下文中一共展示了xmlschema.XMLSchema方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nillable
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_nillable(self):
# Issue #76
xsd_string = """<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="foo" type="Foo" />
<xs:complexType name="Foo">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="bar" type="xs:integer" nillable="true" />
</xs:sequence>
</xs:complexType>
</xs:schema>
"""
xsd_schema = xmlschema.XMLSchema(xsd_string)
xml_string_1 = "<foo><bar>0</bar></foo>"
xml_string_2 = """<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bar xsi:nil="true"></bar>
</foo>
"""
self.assertTrue(xsd_schema.is_valid(source=xml_string_1, use_defaults=False))
self.assertTrue(xsd_schema.is_valid(source=xml_string_2, use_defaults=False))
obj = xsd_schema.decode(xml_string_2, use_defaults=False)
self.check_etree_elements(ElementTree.fromstring(xml_string_2), xsd_schema.encode(obj))
示例2: test_decoding_errors_with_validation_modes
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_decoding_errors_with_validation_modes(self):
schema = self.schema_class("""<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="rootType" />
<xs:complexType name="rootType">
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute name="int_attr" type="xs:int"/>
<xs:attribute name="bool_attr" type="xs:boolean"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="simple_root" type="xs:float"/>
</xs:schema>""")
self.assertIsNone(schema.to_dict("<simple_root>alpha</simple_root>", validation='lax')[0])
self.assertEqual(schema.to_dict("<root int_attr='10'>20</root>"),
{'@int_attr': 10, '$': 20})
self.assertEqual(schema.to_dict("<root int_attr='wrong'>20</root>", validation='lax')[0],
{'@int_attr': None, '$': 20})
self.assertEqual(schema.to_dict("<root int_attr='wrong'>20</root>", validation='skip'),
{'@int_attr': 'wrong', '$': 20})
示例3: test_max_depth_argument
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_max_depth_argument(self):
schema = self.schema_class(self.col_xsd_file)
self.assertEqual(
schema.decode(self.col_xml_file, max_depth=1),
{'@xmlns:col': 'http://example.com/ns/collection',
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xsi:schemaLocation': 'http://example.com/ns/collection collection.xsd'})
xmlschema.limits.MAX_XML_DEPTH = 1
with self.assertRaises(XMLSchemaValidationError):
schema.decode(self.col_xml_file)
xmlschema.limits.MAX_XML_DEPTH = 9999
self.assertEqual(
schema.decode(self.col_xml_file, max_depth=2),
{'@xmlns:col': 'http://example.com/ns/collection',
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xsi:schemaLocation': 'http://example.com/ns/collection collection.xsd',
'object': [{'@id': 'b0836217462', '@available': True},
{'@id': 'b0836217463', '@available': True}]})
示例4: test_cdata_mapping
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_cdata_mapping(self):
schema = XMLSchema("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="node" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
""")
self.assertEqual(
schema.decode('<root>1<node/>2<node/>3</root>'), {'node': [None, None]}
)
self.assertEqual(
schema.decode('<root>1<node/>2<node/>3</root>', cdata_prefix='#'),
{'#1': '1', 'node': [None, None], '#2': '2', '#3': '3'}
)
示例5: test_exception_init
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_exception_init(self):
xs = XMLSchema(os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd'))
with self.assertRaises(ValueError) as ctx:
XMLSchemaValidatorError(xs, 'unknown error', elem='wrong')
self.assertIn("'elem' attribute requires an Element", str(ctx.exception))
error = XMLSchemaNotBuiltError(xs, 'schema not built!')
self.assertEqual(error.message, 'schema not built!')
schema = XMLSchema("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="group1">
<xs:choice>
<xs:element name="root" type="xs:integer"/>
</xs:choice>
</xs:group>
</xs:schema>""")
error = XMLSchemaModelDepthError(schema.groups['group1'])
self.assertEqual("maximum model recursion depth exceeded", error.message[:38])
示例6: test_setattr
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_setattr(self):
schema = XMLSchema("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:integer"/>
</xs:schema>""")
root = ElementTree.XML('<root a="10"/>')
with self.assertRaises(XMLSchemaValidationError) as ctx:
schema.validate(root)
self.assertIsInstance(ctx.exception.source, XMLResource)
self.assertFalse(ctx.exception.source.is_lazy())
resource = XMLResource(io.StringIO('<root a="10"/>'), lazy=True)
with self.assertRaises(XMLSchemaValidationError) as ctx:
schema.validate(resource)
self.assertIsInstance(ctx.exception.source, XMLResource)
self.assertTrue(ctx.exception.source.is_lazy())
self.assertIsNone(ctx.exception.elem)
self.assertEqual(ctx.exception.source, resource)
self.assertEqual(ctx.exception.path, '/root')
示例7: test_properties
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_properties(self):
schema = XMLSchema("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:integer"/>
</xs:schema>""")
root = lxml.etree.XML('<root a="10"/>')
with self.assertRaises(XMLSchemaValidationError) as ctx:
schema.validate(root)
self.assertEqual(ctx.exception.sourceline, 1)
self.assertEqual(ctx.exception.root, root)
xsd_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd')
xs = XMLSchema(xsd_file)
with self.assertRaises(XMLSchemaValidatorError) as ctx:
raise XMLSchemaValidatorError(xs, 'unknown error')
self.assertIsNone(ctx.exception.root)
self.assertIsNone(ctx.exception.schema_url)
self.assertEqual(ctx.exception.origin_url, xs.source.url)
self.assertIsNone(XMLSchemaValidatorError(None, 'unknown error').origin_url)
示例8: _validate_openscenario_configuration
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def _validate_openscenario_configuration(self):
"""
Validate the given OpenSCENARIO config against the 0.9.1 XSD
Note: This will throw if the config is not valid. But this is fine here.
"""
xsd_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../openscenario/OpenSCENARIO.xsd")
xsd = xmlschema.XMLSchema(xsd_file)
xsd.validate(self.xml_tree)
示例9: _validate_openscenario_catalog_configuration
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def _validate_openscenario_catalog_configuration(self, catalog_xml_tree):
"""
Validate the given OpenSCENARIO catalog config against the 0.9.1 XSD
Note: This will throw if the catalog config is not valid. But this is fine here.
"""
xsd_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../openscenario/OpenSCENARIO.xsd")
xsd = xmlschema.XMLSchema(xsd_file)
xsd.validate(catalog_xml_tree)
示例10: _validate
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def _validate(xml, schemapath):
"""Validates given XML file/element with a given schema
:xml: parsed xml element/file
:schemapath: path to a schema file
"""
schema = xmlschema.XMLSchema(schemapath)
for error in schema.iter_errors(xml):
print(error)
return schema.is_valid(xml)
示例11: setUpClass
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def setUpClass(cls):
print("Starting pmml validation tests.")
data = datasets.load_iris()
cls.X = data.data
cls.y = data.target
cls.y_bin = [i%2 for i in range(cls.X.shape[0])]
cls.features = data.feature_names
data = datasets.load_boston()
cls.X_reg = data.data
cls.y_reg = data.target
cls.features_reg = data.feature_names
cls.schema = xmlschema.XMLSchema("nyoka/pmml44.xsd")
cls.statsmodels_data_helper = StatsmodelsDataHelper()
示例12: schema
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def schema():
"""Returns a xmlschema.XMLSchema object for the junit-10.xsd file"""
fn = Path(__file__).parent / "example_scripts/junit-10.xsd"
with fn.open() as f:
return xmlschema.XMLSchema(f)
示例13: test_qname_decoding
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_qname_decoding(self):
schema = self.schema_class("""<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="rootType" />
<xs:complexType name="rootType">
<xs:simpleContent>
<xs:extension base="xs:QName">
<xs:attribute name="name" type="xs:QName"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>""")
xml_data = '<root xmlns:ns0="http://xmlschema.test/0">ns0:foo</root>'
self.assertEqual(schema.decode(xml_data), 'ns0:foo')
self.assertEqual(schema.decode('<root>foo</root>'), 'foo')
with self.assertRaises(XMLSchemaValidationError) as ctx:
schema.decode('<root>ns0:foo</root>')
self.assertIn("failed validating 'ns0:foo'", str(ctx.exception))
self.assertIn("Reason: unmapped prefix 'ns0' on QName", str(ctx.exception))
self.assertIn("Path: /root", str(ctx.exception))
xml_data = '<root name="ns0:bar" xmlns:ns0="http://xmlschema.test/0">ns0:foo</root>'
self.assertEqual(schema.decode(xml_data), {'@name': 'ns0:bar', '$': 'ns0:foo'})
# Check reverse encoding
obj = schema.decode(xml_data, converter=JsonMLConverter)
root = schema.encode(obj, converter=JsonMLConverter)
self.assertEqual(ElementTree.tostring(root), b'<root name="ns0:bar">ns0:foo</root>\n')
with self.assertRaises(XMLSchemaValidationError) as ctx:
schema.decode('<root name="ns0:bar">foo</root>')
self.assertIn("failed validating 'ns0:bar'", str(ctx.exception))
self.assertIn("Reason: unmapped prefix 'ns0' on QName", str(ctx.exception))
self.assertIn("Path: /root", str(ctx.exception))
示例14: test_non_global_schema_path
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_non_global_schema_path(self):
# Issue #157
xs = xmlschema.XMLSchema("""<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:foo="http://example.com/foo"
targetNamespace="http://example.com/foo">
<xs:complexType name="type1">
<xs:sequence>
<xs:element name="sub_part1" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="type2">
<xs:sequence>
<xs:element name="sub_part2" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="part1" type="foo:type1" />
<xs:element name="part2" type="foo:type2" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>""")
self.assertEqual(
xs.to_dict(
"""<part1 xmlns:foo="http://example.com/foo">
<sub_part1>test</sub_part1>
</part1>""",
schema_path='.//part1',
),
{"sub_part1": "test"}
)
示例15: test_dict_granularity
# 需要导入模块: import xmlschema [as 别名]
# 或者: from xmlschema import XMLSchema [as 别名]
def test_dict_granularity(self):
"""Based on Issue #22, test to make sure an xsd indicating list with
dictionaries, returns just that even when it has a single dict. """
xsd_string = self.casepath('issues/issue_022/xsd_string.xsd')
xml_string_1 = self.casepath('issues/issue_022/xml_string_1.xml')
xml_string_2 = self.casepath('issues/issue_022/xml_string_2.xml')
xsd_schema = xmlschema.XMLSchema(xsd_string)
xml_data_1 = xsd_schema.to_dict(xml_string_1)
xml_data_2 = xsd_schema.to_dict(xml_string_2)
self.assertTrue(isinstance(xml_data_1['bar'], type(xml_data_2['bar'])),
msg="XSD with an array that return a single element from "
"xml must still yield a list.")