元素不是模式的元素

Element is not an element of the schema

我想根据 iso20022 XSD 验证我银行的 XML 文件,但它失败了,因为第一个元素 (Document) 不是该方案的元素。不过,我可以看到 XSD 中定义的 'Document' 元素。

我从这里下载了 header 中提到的 XSD:https://www.iso20022.org/documents/messages/camt/schemas/camt.052.001.06.zip 然后我写了一个小脚本来验证 XML 文件:

import xmlschema

schema = xmlschema.XMLSchema('camt.052.001.06.xsd')
schema.validate('minimal_example.xml')

(使用'pip install xmlschema'安装xmlschema包)

minimal_example.xml 只是我银行 XML 文件的第一个元素,没有任何 children.

<?xml version="1.0" ?>
<Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</Document>

上述脚本失败,声称 document 不是 XSD 的元素:

xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element 'Document' at 0x7fbda11e4138> with XMLSchema10(basename='camt.052.001.06.xsd', namespace='urn:iso:std:iso:20022:tech:xsd:camt.052.001.06'):

Reason: <Element 'Document' at 0x7fbda11e4138> is not an element of the schema

Instance:

  <Document>
  </Document>

但是文档元素是在 camt.052.001 的顶部定义的。06.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Standards Editor (build:R1.6.5.6) on 2016 Feb 12 18:17:13, ISO 20022 version : 2013-->
<xs:schema xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
    <xs:element name="Document" type="Document"/>
[...]

为什么验证失败,我该如何纠正?

XSD有

targetNamespace="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06"

xs:schema 元素上,表示它管理该命名空间。

您的 XML 有一个根元素,

<Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</Document>

Document 置于 无命名空间 中。要将其置于 XSD 管辖的命名空间中,请将其更改为

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</Document>

<ns2:Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</ns2:Document>

另见