De aktuelle dataene er ordnet i en XML-fil med korrekte, validerbare data:
Kopier filer og pythonkoden nedenfor og eksperimenter.
Eksperimenter med forskjellige varianter av filer som ikke validerer, brukt som
XMLFILE_UNVALID='all_results_unvalid.xml' i koden nedenfor.
Eksempel
"""
validating with schema
"""
from lxml import etree
"""
testfiles
"""
XMLFILE_OK='../lxml/all_results.xml'
XMLFILE_UNVALID='../lxml/all_results_unvalid.xml'
SCHEMAFILE='../lxml/olymp.xsd'
"""
valid
"""
def testing1():
xml_tree=etree.parse(XMLFILE_OK)
schema_tree=etree.parse(SCHEMAFILE)
schema=etree.XMLSchema(schema_tree)
result=schema.validate(xml_tree)
print (result) #True
"""
not valid
"""
def testing2():
xml_tree=etree.parse(XMLFILE_UNVALID)
schema_tree=etree.parse(SCHEMAFILE)
schema=etree.XMLSchema(schema_tree)
result=schema.validate(xml_tree)
print (result) #False
"""
not valid, with report
"""
def testing3():
xml_tree=etree.parse(XMLFILE_UNVALID)
schema_tree=etree.parse(SCHEMAFILE)
schema=etree.XMLSchema(schema_tree)
try:
schema.assertValid(xml_tree)
except:
log = schema.error_log
error = log.last_error
print (error)
testing1()
testing2()
testing3()
Den siste metoden, testing3(), gir i mitt tilfelle følgende utskrift:
../lxml/all_results_unvalid.xml:7:0:ERROR:SCHEMASV:SCHEMAV_ELEMENT_CONTENT: Element 'result':
This element is not expected. Expected is ( nation ).
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="IOC">
<xs:complexType>
<xs:sequence>
<xs:element ref="OlympicGame" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="OlympicGame">
<xs:complexType>
<xs:sequence>
<xs:element ref="event" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="place" use="required">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="Athens"/>
<xs:enumeration value="Atlanta"/>
<xs:enumeration value="Barcelona"/>
<xs:enumeration value="Sidney"/>
<xs:enumeration value="Beijing"/>
<xs:enumeration value="London"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="year" use="required">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="1992"/>
<xs:enumeration value="1996"/>
<xs:enumeration value="2000"/>
<xs:enumeration value="2004"/>
<xs:enumeration value="2008"/>
<xs:enumeration value="2012"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="athlet">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="nation"/>
<xs:element ref="result"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="event">
<xs:complexType>
<xs:sequence>
<xs:element ref="athlet" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="dist" use="required">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="100m"/>
<xs:enumeration value="200m"/>
<xs:enumeration value="400m"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="name" type="xs:string"/>
<xs:element name="nation" type="xs:string"/>
<xs:element name="result" type="xs:string"/>
</xs:schema>