-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_lxml.py
More file actions
45 lines (29 loc) · 1.06 KB
/
Copy pathvalidation_lxml.py
File metadata and controls
45 lines (29 loc) · 1.06 KB
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
from lxml import etree as ET
xsd_file ='stock.xsd'
xml_file ='stock.xml'
# Parser le document XML
xml_tree = ET.parse (xml_file)
# Parser le document XSD
xsd_schema = ET.parse (xsd_file)
# Créer une instance du document XSD
schema = ET.XMLSchema(xsd_schema)
print('Instance crée !');
#Note that the argument can be also a file-like object or a string containing the schema definition. See examples 1 and 2 bellow :
# Exemple 1:
#schema_file = open('tests/test_cases/examples/collection/collection.xsd')
#schema = xmlschema.XMLSchema(schema_file)
#Example 2 :
#schema = xmlschema.XMLS chema("""
#<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
#<xs:element name="block" type="xs:string"/>
#</xs:schema>
#""")
#if schema.is_valid ('commande.xml') :
# print('Validated with success !')
#else :
#print('contains errors !')
schema.validate(xml_tree)
#Peut aussi être utilisée comme suit :
#fichier_xsd = 'commande1.xsd'
#fichier_xml = 'commande.xml'
#xmlschema.validate (fichier_xml, schema=fichier_xsd)