Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/ucis/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,50 @@


def validate_ucis_xml(file_or_filename):

xml_pkg_dir = os.path.dirname(os.path.abspath(__file__))
schema_dir = os.path.join(xml_pkg_dir, "schema")
ucis_xsd = os.path.join(schema_dir, "ucis.xsd")

with open(ucis_xsd, "r") as xsd_fp:
ucis_xsd_doc = etree.parse(xsd_fp)

# print("schema_doc: " + tounicode(ucis_xsd_doc, pretty_print=True))

ucis_schema = etree.XMLSchema(ucis_xsd_doc)

# print("schema: " + str(ucis_schema))

if type(file_or_filename) == str:
print("open file")
fp = open(file_or_filename, "r")
else:
fp = file_or_filename

ret = False

try:
doc = etree.parse(fp)

root = doc.getroot()

# There is some inconsistency in whether
# elements should be namespace-qualified or not.
# The official schema indicates that they should be,
# while the examples indicate they shouldn't be. The
# (apparently) simplest way around this is to remove
# (apparently) simplest way around this is to remove
# namespace qualification entirely.
for elem in root.getiterator():
if not hasattr(elem.tag, 'find'): continue # (1)
i = elem.tag.find('}')
if i >= 0:
elem.tag = elem.tag[i+1:]
elem.tag = elem.tag[i+1:]

ret = ucis_schema.assertValid(doc)
ret = True

finally:
if type(file_or_filename) == str:
fp.close()

return ret
return ret