gpt4 book ai didi

xml - 验证中的 "Elements ... does not resolve to a(n) type definition"

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:06 25 4
gpt4 key购买 nike

我正在尝试创建一个 XSD 文件作为过滤器来验证一些必须进一步处理的 XML 文件。

这是 XSL 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<acknowledgement xmlns="http://sungardams.com/Validation.xsd" xmlns:common="http://sungardams.com/common.xsd">
<type>POSITIVE</type> <!-- must have value POSITIVE -->
<originReference>
<externalMessageId>12345678-010</externalMessageId>
</originReference>
<requestMessageId>000000000000000000000000001</requestMessageId>
<senderInfo>
<common:messageId>000000000000000000000000000001</common:messageId>
<common:externalMessageType>securityAddRequest</common:externalMessageType> <!-- must have value securityAddRequest -->
<common:applicationHistory>
<common:originatorReference>
<common:originator>GLOBAL PLUS</common:originator> <!-- must have value GLOBAL PLUS -->
<common:reference>ABCDE001</common:reference>
<common:primaryReferenceType>GREF</common:primaryReferenceType> <!-- must have value GREF -->
</common:originatorReference>
</common:applicationHistory>
</senderInfo>
</acknowledgement>

我收到的文件使用另一个 XSD 文件进行验证,并使用命名空间 common(解释为什么某些元素以 common: 为前缀)。

所以我创建了以下 XSD 文件:

验证.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:common="http://sungardams.com/common.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://sungardams.com/common.xsd" schemaLocation="http://sungardams.com/common.xsd antCommon001.xsd"/>

<xs:element name="acknowledgement">
<xs:complexType>
<xs:sequence>
<xs:element name="type" type="xs:string" fixed="POSITIVE" />
<xs:element name="originReference">
<xs:complexType>
<xs:sequence>
<xs:element name="externalMessageId" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="requestMessageId" type="xs:string" />
<xs:element name="senderInfo" type="CType_SenderInfo_ant" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

最初,元素senderInfo 是在这个文件中定义的。但是当我那样尝试时,我会收到错误消息,指出我的元素无效(我会在名称前加上命名空间 common:,所以会收到消息说它们无效有效的 xs:NCName)。

所以我将发件人信息移到了另一个文件中:antCommon001.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="CType_SenderInfo_ant">
<xs:complexType>
<xs:sequence>
<xs:element name="messageId" type="xs:string" />
<xs:element name="externalMessageType" type="xs:string" fixed="securityAddRequest" />
<xs:element name="applicationHistory">
<xs:complexType>
<xs:sequence>
<xs:element name="originatorReference">
<xs:complexType>
<xs:sequence>
<xs:element name="originator" type="xs:string" fixed="GLOBAL PLUS" />
<xs:element name="reference" type="xs:string" />
<xs:element name="primaryReferenceType" type="xs:string" fixed="GREF" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

现在,当我运行 XML 文件的验证时,我收到消息(使用 Notepad++ XML 工具验证插件):

Unable to parse schema file ... element decl. 'senderInfo', attribute 'type': The QName value 'CType_SenderInfo_ant' does not resolve to a(n) type definition.

我做错了什么?

最佳答案

您必须进行多项更改,包括:

  • xs:import/@schemaLocation 不应是 namespace -XSDurl 对,因为它用于 xs:schema/@schemaLocation——它应该只是指向的 URLXSD。
  • 要引用另一个 namespace 中的类型,请在其前面加上 namespace 为该 namespace 声明的前缀。
  • antCommon.XSD 在您似乎希望的时候声明一个元素而是引用一个类型

总而言之,通过一些额外的修复,以下对您的 XML 和 XSD 文件的更新将成功验证您的 XML:

验证.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<acknowledgement xmlns="http://sungardams.com/Validation.xsd"
xmlns:common="http://sungardams.com/common.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://sungardams.com/Validation.xsd Validation.xsd">
<type>POSITIVE</type> <!-- must have value POSITIVE -->
<originReference>
<externalMessageId>12345678-010</externalMessageId>
</originReference>
<requestMessageId>000000000000000000000000001</requestMessageId>
<senderInfo>
<common:messageId>000000000000000000000000000001</common:messageId>
<common:externalMessageType>securityAddRequest</common:externalMessageType> <!-- must have value securityAddRequest -->
<common:applicationHistory>
<common:originatorReference>
<common:originator>GLOBAL PLUS</common:originator> <!-- must have value GLOBAL PLUS -->
<common:reference>ABCDE001</common:reference>
<common:primaryReferenceType>GREF</common:primaryReferenceType> <!-- must have value GREF -->
</common:originatorReference>
</common:applicationHistory>
</senderInfo>
</acknowledgement>

验证.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:common="http://sungardams.com/common.xsd"
targetNamespace="http://sungardams.com/Validation.xsd"
elementFormDefault="qualified">
<xs:import namespace="http://sungardams.com/common.xsd"
schemaLocation="antCommon.xsd"/>

<xs:element name="acknowledgement">
<xs:complexType>
<xs:sequence>
<xs:element name="type" type="xs:string" fixed="POSITIVE" />
<xs:element name="originReference">
<xs:complexType>
<xs:sequence>
<xs:element name="externalMessageId" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="requestMessageId" type="xs:string" />
<xs:element name="senderInfo" type="common:CType_SenderInfo_ant" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

antCommon.XSD

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://sungardams.com/common.xsd"
elementFormDefault="qualified">

<xs:complexType name="CType_SenderInfo_ant">
<xs:sequence>
<xs:element name="messageId" type="xs:string" />
<xs:element name="externalMessageType" type="xs:string"
fixed="securityAddRequest" />
<xs:element name="applicationHistory">
<xs:complexType>
<xs:sequence>
<xs:element name="originatorReference">
<xs:complexType>
<xs:sequence>
<xs:element name="originator" type="xs:string"
fixed="GLOBAL PLUS" />
<xs:element name="reference" type="xs:string" />
<xs:element name="primaryReferenceType"
type="xs:string" fixed="GREF" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

</xs:schema>

关于xml - 验证中的 "Elements ... does not resolve to a(n) type definition",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41879602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com