Search code examples
xmlxsdxml-validation

"Elements ... does not resolve to a(n) type definition" in validation


I am trying to create an XSD file to work as a filter to validate some XML files that will have to be processed further.

Here is the XSL file:

<?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>

The file I receive is validated using another XSD file, and uses the namespace common (to explain why some elements are prefixed common:).

So I created the following XSD files:

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>

At first, the element senderInfo was defined in this file. But when I tried it like that, I would get the error message that I the elements weren't valid (I would prefixe the name with the namespace common:, so would get the message that they were not valid xs:NCName).

So I moved the sender info into another file: 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>

Now, when I run the validation of the XML file, I get the message (using Notepad++ XML Tools validation plugin):

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

What am I doing wrong?


Solution

  • There are multiple changes you'll have to make, including:

    • xs:import/@schemaLocation should not be a namespace-XSDurl pair as it is for xs:schema/@schemaLocation -- it should just be the URL to the XSD.
    • To reference a type in another namespace, prefix it with a namespace prefix declared to that namespace.
    • antCommon.XSD is declaring an element when you seem to wish to reference a type instead.

    Altogether, with some additional fixes, the following updates to your XML and XSD files will validate your XML successfully:

    validation.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>
    

    Validation.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>