我试图使用 python 验证 XMl 文件 aginst xsd

I was trying to Validate XMl files aginst xsd using python

提问人:Nisha naik 提问时间:11/10/2022 最后编辑:ukBazNisha naik 更新时间:11/10/2022 访问量:116

问:

这是我的XML文件:

\<Orderdetails\>
\<Customer id="011"\>
\<cname\>Mark Lewis\</cname\>
\<destination Country="Australia" Delivdate=" 6 days"\>Free Delivery\</destination\>
\<eid\>[email protected]\</eid\>
\</Customer\>
\<Customer id="022"\>
\<fname from="jersey"\>Loafther cris\</fname\>
\<destination Country="USA" Delivdate=" 11 days"\>Shipping Charges\</destination\>
\<eid\>[email protected]\</eid\>
\</Customer\>
\<Customer id="033"\>
\<cname\>Amal Raj\</cname\>
\<destination Country="Thailand" Delivdate=" 6 days"\>Shipping Charges\</destination\>
\<email\>[email protected]\</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>

错误:

出现以下错误:

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

我在运行过程中遇到的错误

法典: 我正在尝试使用下面编写的代码进行验证:

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)

随着路径中的双斜杠,它变得更远,但现在出现以下错误:

ModuleNotFoundError: No module named 'validator'
Python XML 验证 XSD LXML

评论

1赞 Michael Kay 11/10/2022
所有这些反斜杠是否真的存在于您的 XML 中,或者它们是虚假的?
0赞 mzjn 11/10/2022
删除该行。验证程序库不用于 XSD 验证:pypi.org/project/validatorfrom validator import validate
0赞 Dijkgraaf 11/30/2022
您可以使用十六进制编辑器并判断文件中的前 3 个字节是什么吗?

答: 暂无答案