我正在尝试创建一个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文件:
Validation.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 Tools验证插件):
无法解析模式文件...元素decl。 &#39; senderInfo&#39;,属性&#39;键入&#39;:QName值&#39; CType_SenderInfo_ant&#39;不解析为(n)类型定义。
我做错了什么?
答案 0 :(得分:1)
您需要做出多项更改,包括:
xs:import/@schemaLocation
不应该是名称空间-XSDurl对
它适用于xs:schema/@schemaLocation
- 它应该只是URL
XSD。总之,通过一些其他修补程序,对XML和XSD文件的以下更新将成功验证您的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>
<?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>
<?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>