1
votes

Following is the xsd.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:dmb="http://hmh.mycompany.com/schema/hmh/Message"
    targetNamespace="http://hmh.mycompany.com/schema/hmh/Message"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    version="1.0">

    <xsd:complexType name="Message">
        <xsd:annotation>
            <xsd:documentation>This represents the message 
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:element name="provider" type="xsd:string" />
            <xsd:element name="product" type="xsd:string" />
            <xsd:element name="status" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

And following is the xml file.

<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://hmh.mycompany.com/schema/hmh/Message">
    <provider>PRODUCT</provider>
    <product></product>
    <status></status>
</Message>

When try to validate it using http://www.utilities-online.info/xsdvalidation, I got following error

Line 2, 62: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 62; cvc-elt.1: Cannot find the declaration of element 'Message'.

1

1 Answers

1
votes

You are very close. In addition to defining the Message type, you also have to declare the Message element itself. You can do so by adding,

<xsd:element name="Message" type="dmb:Message"/>

to your XSD. Then your XML will be valid against your XSD.


If you cannot change the XSD, then you have two other options:

  1. Write a new XSD that defines the Message element as I show above, and xsd:include the type definition from the fixed XSD. Use this new XSD to validate your XML.
  2. Use xsi:type in the XML file. (See How to restrict the value of an XML element using xsi:type in XSD?)