【问题标题】:Element is not an element of the schema元素不是架构的元素
【发布时间】:2020-01-10 04:56:45
【问题描述】:

我想针对 iso20022 XSD 验证银行中的 XML 文件,但它声称第一个元素 (Document) 不是该方案的元素时失败。不过,我可以看到 XSD 中定义的“文档”元素。

我从这里下载了标题中提到的 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 文件的第一个元素,没有任何子元素。

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

为什么验证失败,我该如何纠正这个问题?

【问题讨论】:

    标签: python xml xsd xml-validation python-xmlschema


    【解决方案1】:

    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?

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多