You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python验证XML与XSD时遭遇路径及模块导入错误

解决XML XSD验证的Python代码问题

问题场景

我尝试用Python验证XML是否符合XSD规范,相关文件和代码如下:

XML文件内容

<Orderdetails>
<Customer id="011">
<cname>Mark Lewis</cname>
<destination Country="Australia" Delivdate=" 6 days">Free Delivery</destination>
<eid>MarkLew@hotmail.com</eid>
</Customer>
<Customer id="022">
<fname from="jersey">Loafther cris</fname>
<destination Country="USA" Delivdate=" 11 days">Shipping Charges</destination>
<eid>Loafther@email.com</eid>
</Customer>
<Customer id="033">
<cname>Amal Raj</cname>
<destination Country="Thailand" Delivdate=" 6 days">Shipping Charges</destination>
<email>Anamljjk@ymail.com</email>
</Customer>
</Orderdetails>

XSD文件内容

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Orderdetails">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="fname">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="from" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="cname" type="xs:string" />
<xs:element name="destination">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Country" type="xs:string" use="required" />
<xs:attribute name="Delivdate" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="email" type="xs:string" />
<xs:element minOccurs="0" name="eid" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

初始代码及错误

运行以下代码时:

from lxml import etree
from validator import validate

def validate(xml_path: str, xsd_path: str) -> bool:

    xmlschema_doc = etree.parse(xsd_path)
    xmlschema = etree.XMLSchema(xmlschema_doc)
    xml_doc = etree.parse(xml_path)
    result = xmlschema.validate(xml_doc)

    return result
    
    
xml_path="C:\Users\nisha\Downloads\xmlfile\demo.xml"
xsd_path="C:\Users\nisha\Downloads\xsdfile\demo.xsd"

value=validate(xml_path,xsd_path)
print(value)

首先遇到错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

将路径改为双斜杠后,又出现新错误:

ModuleNotFoundError: No module named 'validator'

解决方案

1. 解决Unicode转义错误

Windows路径中的反斜杠\在Python字符串中会被当作转义字符,导致解析错误。三种可行解决方式:

  • 使用双反斜杠表示路径:"C:\\Users\\nisha\\Downloads\\xmlfile\\demo.xml"
  • 使用原始字符串(在字符串前加r):r"C:\Users\nisha\Downloads\xmlfile\demo.xml"
  • 使用正斜杠代替反斜杠:"C:/Users/nisha/Downloads/xmlfile/demo.xml"

2. 解决ModuleNotFoundError错误

代码中from validator import validate属于冗余代码——你已经自行定义了同名的validate函数,无需导入外部validator模块,直接删除该行即可。

修正后的完整代码

from lxml import etree

def validate(xml_path: str, xsd_path: str) -> bool:
    xmlschema_doc = etree.parse(xsd_path)
    xmlschema = etree.XMLSchema(xmlschema_doc)
    xml_doc = etree.parse(xml_path)
    # 可选:打印验证过程中的错误详情
    if not xmlschema.validate(xml_doc):
        print(xmlschema.error_log)
    return xmlschema.validate(xml_doc)

# 使用原始字符串避免转义问题
xml_path = r"C:\Users\nisha\Downloads\xmlfile\demo.xml"
xsd_path = r"C:\Users\nisha\Downloads\xsdfile\demo.xsd"

value = validate(xml_path, xsd_path)
print("验证结果:", value)

额外说明

运行代码前确保已安装lxml库,未安装则执行以下命令:

pip install lxml

内容的提问来源于stack exchange,提问作者Nisha naik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:21:00