使用Notepad++验证XML时遇无效架构/缺失命名空间问题求助
XML与XSD验证错误排查及修复
遇到的错误
- Notepad++验证提示:
Invalid schema or missing namespace - 其他工具检测到两类错误:
- XML文档错误:
cvc-elt.1: Cannot find the declaration of element 'SecondPillarPensionVAPZDeclarationsFile' - XSD文件错误:
TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'http://www.sigedis.be/db2p/declarations/vapz/v1'
- XML文档错误:
问题根源分析
- XSD命名空间不一致:XSD文件中默认命名空间使用
https://协议,但targetNamespace使用http://协议,两者不匹配,导致XML无法关联到正确的XSD元素定义。 - XML根元素冗余属性:XML根元素上的
namespace属性是无效的,XML的命名空间应通过xmlns声明,该冗余属性可能干扰验证逻辑。 - 命名空间引用不匹配:XML中
person前缀对应的命名空间与XSD中导入的common命名空间不一致,可能导致子元素验证失败。
修复步骤
1. 修正XSD的默认命名空间
将XSD根元素的默认命名空间从https://www.sigedis.be/db2p/declarations/vapz/v1修改为http://www.sigedis.be/db2p/declarations/vapz/v1,与targetNamespace保持完全一致。
2. 删除XML根元素的冗余属性
移除XML根元素上的namespace="http://www.sigedis.be/db2p/declarations/vapz/v1"属性,保留xmlns声明即可。
3. 对齐命名空间引用(可选但必要)
XML中person前缀对应的命名空间是http://kszbcss.fgov.be/types/DB2P/Common/Identification/Person/v3,但XSD中仅导入了http://kszbcss.fgov.be/types/DB2P/Common/v3。需确认是否存在对应Identification/Person/v3的XSD文件,若存在则在XSD中添加对应的xs:import;若不存在则调整XML的person命名空间为XSD已导入的http://kszbcss.fgov.be/types/DB2P/Common/v3。
修正后的代码示例
修正后的XML
<?xml version="1.0" encoding="UTF-8"?> <SecondPillarPensionVAPZDeclarationsFile xmlns="http://www.sigedis.be/db2p/declarations/vapz/v1" xmlns:person="http://kszbcss.fgov.be/types/DB2P/Common/Identification/Person/v3" xmlns:db2p-declarations-common="http://www.sigedis.be/db2p/declarations/common/v1"> <AdministrativeData> <db2p-declarations-common:DeclarationFileId>000000002</db2p-declarations-common:DeclarationFileId> <db2p-declarations-common:Sender>0000000000</db2p-declarations-common:Sender> <db2p-declarations-common:Registrant>0000000000</db2p-declarations-common:Registrant> <db2p-declarations-common:CreationMoment>2024-04-01T00:42:06+01:00</db2p-declarations-common:CreationMoment> <db2p-declarations-common:Environment>DB2PSIM</db2p-declarations-common:Environment> </AdministrativeData> <Declarations> <DeclarationVAPZ> <CreateAccountVAPZ sequence="1"> <RegistrantId>BAL-VA00000000</RegistrantId> <Regulation> <SigedisId>2000-0000-0000-0000-0000-0000</SigedisId> </Regulation> <Affiliate> <person:Ssin>00000000000</person:Ssin> <person:PersonData> <person:Name> <person:Name>TEST</person:Name> <person:FirstName>TEST</person:FirstName> </person:Name> <person:Birth> <person:Date>1990-01-01</person:Date> <person:BelgianLocation> <person:Municipality>ANTWERPEN</person:Municipality> </person:BelgianLocation> </person:Birth> <person:Gender>F</person:Gender> </person:PersonData> </Affiliate> <AffiliationDate>2024-04-01</AffiliationDate> <InvestementTypeReservesInsurer>NoFund</InvestementTypeReservesInsurer> </CreateAccountVAPZ> </DeclarationVAPZ> </Declarations> </SecondPillarPensionVAPZDeclarationsFile>
修正后的XSD
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sigedis.be/db2p/declarations/vapz/v1" xmlns:db2p-declarations-common="http://www.sigedis.be/db2p/declarations/common/v1" xmlns:common="http://kszbcss.fgov.be/types/DB2P/Common/v3" xmlns:person="http://kszbcss.fgov.be/types/DB2P/Common/Identification/Person/v3" targetNamespace="http://www.sigedis.be/db2p/declarations/vapz/v1" elementFormDefault="qualified" version="1.6.3"> <xs:import namespace="http://kszbcss.fgov.be/types/DB2P/Common/v3" schemaLocation="../../Common/Common_v3.xsd"/> <xs:import namespace="http://kszbcss.fgov.be/types/DB2P/Common/Identification/Person/v3" schemaLocation="../../Common/Identification/Person_v3.xsd"/> <xs:import namespace="http://www.sigedis.be/db2p/declarations/common/v1" schemaLocation="../common/db2pDeclarationsCommonBaseComponents_v1.xsd"/> <xs:include schemaLocation="db2pDeclarationsVAPZDeclaration_v1.xsd"/> <xs:element name="SecondPillarPensionVAPZDeclarationsFile"> <xs:complexType> <xs:sequence> <xs:element name="AdministrativeData" type="db2p-declarations-common:AdministrativeDataRequestType"/> <xs:element name="Declarations" type="DeclarationsTypeVAPZ"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="SecondPillarPensionVAPZDeclarationsResponseFile"> <xs:annotation> <xs:documentation>Dit element beschrijft het antwoord voor de pensioeninstelling</xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name="InformationCustomer" type="common:InformationCustomerType"> <xs:annotation> <xs:documentation>Information block that will be used by the integrator for diagnostic use.</xs:documentation> </xs:annotation> </xs:element> <xs:element name="InformationCBSS" type="common:InformationCBSSType"> <xs:annotation> <xs:documentation>Information block for CBSS.</xs:documentation> </xs:annotation> </xs:element> <xs:element name="AdministrativeData" type="db2p-declarations-common:AdministrativeDataResponseType"/> <xs:element name="Status" type="common:StatusType"/> <xs:element name="DeclarationsResponses" type="DeclarationsResponsesTypeVAPZ" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
内容的提问来源于stack exchange,提问作者Nick Janssen
相关产品推荐
相关产品推荐

