元素不是架构的元素

2024-04-20 06:39:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用iso20022 XSD验证我的银行的XML文件,但它无法声明第一个元素(Document)不是方案的元素。不过,我可以看到XSD中定义的“Document”元素。你知道吗

我从这里下载了头文件中提到的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包)

最小_示例.xml只是我的银行XML文件的第一个元素,没有任何子元素。你知道吗

<?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"/>
[...]

为什么验证失败?我如何更正?你知道吗


Tags: 文件元素versionschemaisoxmldocumenttech
1条回答
网友
1楼 · 发布于 2024-04-20 06:39:14

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>

另见xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

相关问题 更多 >