Xml &引用;元素。。。不解析为(n)类型定义“;有效

Xml &引用;元素。。。不解析为(n)类型定义“;有效,xml,xsd,xml-validation,Xml,Xsd,Xml Validation,我试图创建一个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>

我试图创建一个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>
首先,元素
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工具验证插件):

无法分析架构文件。。。元素decl'senderInfo',属性“type”:QName值“CType\u senderInfo\u ant”未解析为(n)类型定义


我做错了什么?

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

  • xs:import/@schemaLocation
    不应作为命名空间XSDurl对 它用于
    xs:schema/@schemaLocation
    ——它应该只是 XSD
  • 要引用另一个命名空间中的类型,请在其前面加一个命名空间 为该命名空间声明的前缀
  • antCommon.XSD在您希望的时候声明了一个元素 改为引用类型
总之,通过一些额外的修复,以下对XML和XSD文件的更新将成功验证您的XML:

validation.xml

肯定的
12345678-010
000000000000000000000000001
000000000000000000000000000001
securityAddRequest
全球加号
ABCDE001
格雷夫
Validation.xsd

antCommon.XSD


Hi@kjhughes。谢谢你的回答。我尝试为您的文件更改我的文件,但它为同一行提供了相同的错误消息(“QName值“{}CType_SenderInfo_ant”未解析为(n)类型定义。”)。这可能是由于某个文件的版本不同吗?取决于XSD之间的相对位置,您可能需要调整
xs:import/@schemaLocation
值。在我看来,它是相对于主XSD的。如果相对路径给您带来麻烦,请尝试使用绝对路径。另外,请参阅我在同一文件夹中的所有文件中提供的提示。好的,现在它给出的消息是“ERROR:Element'{}acknowledge:验证根目录没有匹配的全局声明”
Validation.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 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>