使用lxml.etree.XMLSchema验证XML是否符合XSD,需先加载XSD构建验证器,再解析XML并调用validate()或assertValid()校验,注意命名空间、编码及导入包含路径处理。
用 lxml 验证 XML 是否符合 XSD,核心是使用 lxml.etree.XMLSchema 类加载 XSD 文件,再用它校验 XML 文档。关键在于正确处理命名空间、解析错误和验证失败信息。
先用 etree.parse() 读取 XSD 文件,再传给 XMLSchema() 构造验证器对象。注意:XSD 必须是有效格式,且路径可访问。
)或包含(),确保相关文件路径正确,或通过 etree.Resolver 自定义解析逻辑etree.XMLParser(resolve_entities=False))避免实体解析干扰from lxml import etree
with open("schema.xsd", "rb") as f:
schema_root = etree.XML(f.read())
schema = etree.XMLSchema(schema_root)
XML 文件需先解析为 etree.ElementTree 或 etree.Element,再调用验证器的 .validate() 方法。该方法返回布尔值,但不抛异常——错误信息需主动获取。
schema.assertValid(doc) 可直接抛出 DocumentInvalid 异常,适合严格校验场景schema.validate(doc) + schema.error_log 获取详细报错with open("data.xml", "rb") as f:
doc = etree.parse(f)
if not schema.validate(doc):
for error in schema.error_log:
print(f"Line {error.line}, Col {error.column}: {error.message}")
XSD 和 XML 中的命名空间不匹配是验证失败的主因之一;同时注意文件编码(建议统一用 UTF-

,XSD 同理xmlns="http://example.com/ns"),XSD 中 targetNamespace 必须一致,且元素引用需带前缀或正确设置 elementFormDefault="qualified"
print(etree.tostring(doc, encoding="unicode", pretty_print=True)) 检查实际解析结构封装一个健壮的验证函数,兼顾异常捕获、日志输出和返回结构化结果:
def validate_xml_with_xsd(xml_path, xsd_path):
try:
with open(xsd_path, "rb") as f:
schema_root = etree.XML(f.read())
schema = etree.XMLSchema(schema_root)
except etree.XMLSyntaxError as e:
return {"valid": False, "error": f"XSD 解析失败: {e}"}
try:
with open(xml_path, "rb") as f:
doc = etree.parse(f)
except etree.XMLSyntaxError as e:
return {"valid": False, "error": f"XML 解析失败: {e}"}
if not schema.validate(doc):
errors = [
{"line": e.line, "column": e.column, "message": e.message}
for e in schema.error_log
]
return {"valid": False, "errors": errors}
return {"valid": True}