File: extra_validator.py

package info (click to toggle)
python-xmlschema 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,208 kB
  • sloc: python: 39,174; xml: 1,282; makefile: 36
file content (33 lines) | stat: -rw-r--r-- 911 bytes parent folder | download | duplicates (2)
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
from typing import Iterator, Optional
from xml.etree import ElementTree
import xmlschema

document = ElementTree.fromstring("<id>http://example.org</id>")
schema = xmlschema.XMLSchema11("""\
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="id" type="xsd:anyURI" />
</xsd:schema>
""")


def extra_validator1(
    element: ElementTree.Element,
    xsd_element: xmlschema.XsdElement,
) -> Optional[Iterator[xmlschema.XMLSchemaValidationError]]:
    _ = element.tag, xsd_element.type.name
    return None


schema.validate(document, extra_validator=extra_validator1)


def extra_validator2(
    element: ElementTree.Element,
    xsd_element: xmlschema.XsdElement,
) -> Optional[Iterator[xmlschema.XMLSchemaValidationError]]:
    _ = element.tag, xsd_element.type.name
    return None


schema.validate(document, extra_validator=extra_validator2)