#!/usr/bin/env python3 """Валидация XML-частей samples/*.docx против XSD ECMA-376 (transitional). Схемы — docs/standards/docx/schema/transitional/ (полный набор ECMA-376 5-го изд.). wml.xsd — word/*.xml (document/styles/settings/header/footer/...); dml-chart.xsd — word/charts/chart*.xml; dml-diagram.xsd — word/diagrams/*; dml-main.xsd — word/theme/theme*.xml. Два допущения, оба по ECMA-376 Part 1: * xml:space — wml.xsd/shared-math.xsd ссылаются на атрибут xml:space без импорта XML-namespace (§5.1.2: xml:* — предопределённые атрибуты); XSD-линтеру нужен явный импорт — подмешиваем xml.xsd в копию схемы в tmp. * mc:AlternateContent (§15.2.15) — MC-элементы допускаются в любом расширяемом содержимом и не моделируются схемой; валидируем Fallback- ветку (Choice — расширение, необязательное для conformant-чтения). """ import os, re, sys, shutil, tempfile, zipfile from lxml import etree REPO = os.path.dirname(os.path.abspath(__file__)) SCHEMA_DIR = os.path.join(REPO, "schema", "transitional") XML_XSD = """ """ _cache = {} _tmp = None def tmpdir(): global _tmp if _tmp is None: _tmp = tempfile.mkdtemp(prefix="ecma-xsd-") shutil.copytree(SCHEMA_DIR, _tmp, dirs_exist_ok=True) with open(os.path.join(_tmp, "xml.xsd"), "w") as f: f.write(XML_XSD) for name in ("wml.xsd", "shared-math.xsd"): p = os.path.join(_tmp, name) s = open(p).read() if 'schemaLocation="xml.xsd"' not in s: if "xmlns:xml=" not in s: s = re.sub(r"(]*)(>)", r'\1 xmlns:xml="http://www.w3.org/XML/1998/namespace"\2', s, count=1) s = re.sub(r"(]*>)", r'\1', s, count=1) with open(p, "w") as f: f.write(s) return _tmp def schema_for(name): if re.match(r"word/(document|footnotes|endnotes|styles|settings|fontTable" r"|webSettings|numbering|(?:header|footer)\d+)\.xml$", name): return "wml.xsd" if re.match(r"word/charts/chart\d+\.xml$", name): return "dml-chart.xsd" # drawing*.xml — dsp: (2008 diagram drawing, [MS-ODRAWXML]) — вне ECMA, не маппится. if re.match(r"word/diagrams/(data|layout|quickStyle|colors)\d+\.xml$", name): return "dml-diagram.xsd" if re.match(r"word/theme/theme\d+\.xml$", name): return "dml-main.xsd" return None MC = "{http://schemas.openxmlformats.org/markup-compatibility/2006}" def strip_mc(el): """MC-обработка по ECMA-376 §15.2.15 перед XSD-валидацией. * mc:AlternateContent -> mc:Fallback-дети (§15.2.15.2); * mc:Ignorable (§15.2.15.1) — перечисленные префиксы: consumer, не понимающий namespace, ОБЯЗАН игнорировать его атрибуты и элементы -> снимаем атрибуты и вырезаем элементы этих namespace'ов; * сами mc:*-атрибуты (Ignorable/Preserve/ProcessAttributes) — не часть схемы, снимаем. """ ignorable = set() for e in el.iter(): ig = e.get(f"{MC}Ignorable") if ig: for tok in ig.split(): ns = e.nsmap.get(tok) if ns: ignorable.add(ns) for e in el.iter(): for a in list(e.attrib): ns = a[1:a.index("}")] if a.startswith("{") else "" if a.startswith(MC) or ns in ignorable: del e.attrib[a] for ns in ignorable: for e in list(el.iter(f"{{{ns}}}*")): parent = e.getparent() if parent is not None: parent.remove(e) for ac in list(el.iter(f"{MC}AlternateContent")): parent = ac.getparent() if parent is None: continue fb = ac.find(f"{MC}Fallback") idx = list(parent).index(ac) parent.remove(ac) if fb is not None: for i, child in enumerate(list(fb)): parent.insert(idx + i, child) def get_schema(fname): if fname not in _cache: _cache[fname] = etree.XMLSchema(etree.parse(os.path.join(tmpdir(), fname))) return _cache[fname] def validate(docx): problems, skipped = [], [] with zipfile.ZipFile(docx) as z: for name in z.namelist(): if not name.endswith(".xml"): continue sf = schema_for(name) if sf is None: if name.startswith("word/") and "rels" not in name: skipped.append(name) continue try: doc = etree.fromstring(z.read(name)) except etree.XMLSyntaxError as e: problems.append((name, f"not well-formed: {e}")) continue strip_mc(doc) try: schema = get_schema(sf) except Exception as e: problems.append((name, f"schema load failed: {e}")) continue if not schema.validate(doc): for err in schema.error_log: problems.append((name, f"{err.message} (line {err.line})")) return problems, skipped rc = 0 for docx in sys.argv[1:]: problems, skipped = validate(docx) print(("OK " if not problems else "FAIL ") + os.path.basename(docx) + (f" (без XSD: {', '.join(skipped)})" if skipped else "")) for name, msg in problems[:40]: print(f" {name}: {msg}") if len(problems) > 40: print(f" ... ещё {len(problems)-40}") if problems: rc = 1 sys.exit(rc)