在Python中验证XML订单
我有一个XML架构文件,该文件是为了示例的目的,我将使用 w3学校的一个如下:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我想使用它来验证具有相同元素的XML文件,并且定义与XSD如下:
<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/schema.xsd">
<to>Tove</to>
<heading>Reminder</heading>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
问题是,在示例中,服务器在示例中将元素发送出顺序,因此,当我运行代码时,它告诉我XML无效,即使我只有有效的元素。
import xmlschema
xsd_file = "schema.xsd"
xml_file = "input.xml"
xmlschema.validate(xml_file, xsd_file)
有没有办法可以在Python中验证此输入,忽略了元素出现的顺序?
执行该代码时,这是当前错误消息:
xmlschemachildrenvalidationError:失败验证&lt; element'{https://www.w3schools.com}请注意'0x000002b13c7e7090&gt; gt;使用XSDGroup(model ='sequence',发生= [1,1]):
原因:带标签'{https://www.w3schools.com}的意外孩子}'在位置2。
架构:
<xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:sequence>
<xs:element name="to" type="xs:string" />
<xs:element name="from" type="xs:string" />
<xs:element name="heading" type="xs:string" />
<xs:element name="body" type="xs:string" />
</xs:sequence>
</xs:complexType>
实例:
<note xmlns="https://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com/schema.xsd">
<to>Tove</to>
<heading>Reminder</heading>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
Path: /note
I have an XML Schema file that for the purpose of the example I'll use W3 school's one as follows:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And I want to use it to validate an XML file that has the same elements, and definitions as the XSD like the following:
<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/schema.xsd">
<to>Tove</to>
<heading>Reminder</heading>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
The problem is, as, in the example, the server sends the elements out of order, so, when I run the code bellow, it tells me that the XML is invalid, even though I only have valid elements.
import xmlschema
xsd_file = "schema.xsd"
xml_file = "input.xml"
xmlschema.validate(xml_file, xsd_file)
Is there a way that I can validate this input in python ignoring the order that the elements appear on?
This is the current error message when executing that code:
XMLSchemaChildrenValidationError: failed validating <Element '{https://www.w3schools.com}note' at 0x000002B13C7E7090> with XsdGroup(model='sequence', occurs=[1, 1]):
Reason: Unexpected child with tag '{https://www.w3schools.com}heading' at position 2. Tag 'from' expected.
Schema:
<xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:sequence>
<xs:element name="to" type="xs:string" />
<xs:element name="from" type="xs:string" />
<xs:element name="heading" type="xs:string" />
<xs:element name="body" type="xs:string" />
</xs:sequence>
</xs:complexType>
Instance:
<note xmlns="https://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3schools.com/schema.xsd">
<to>Tove</to>
<heading>Reminder</heading>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
Path: /note
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论