Exception in schema validation

Discussions

XML & Web services: Exception in schema validation

  1. Exception in schema validation (6 messages)

    Hi All
     
    I am trying to validate xml string against the schema but i am getting a exception. Can anybody tell me what's wrong.

    XML String
    ***********
    pStrXMLSring =
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns1:correspondence-message-response xmlns:ns1="http://www.insystems.com/correspondence12"> id="100_200_C"><ns1:success><ns1:letter-generation-response letter-id="591610101010103810"></ns1:letter-generation-response><ns1:warnings><ns1:corr-warning code="compNotEff" description="Letter Component 'Not Effective Component' has invalid effective date range and is not included within generated letter"></ns1:corr-warning><ns1:corr-warning code="addinNotEff" description="Attachment 'Not Effective Addin' has invalid effective date range and is not included within generated letter"></ns1:corr-warning></ns1:warnings></ns1:success></ns1:response><ns1:response id="100_200_B"><ns1:failure><ns1:errors><ns1:error code="compNotEff" description="Letter Component 'Not Effective Component' has invalid effective date range and is not included within generated letter"></ns1:error><ns1:error code="compNotEff" description="Letter Component 'Not Effective Component' has invalid effective date range and is not included within generated letter"></ns1:error></ns1:errors></ns1:failure></ns1:response></ns1:correspondence-message-response>

    Validation code
    try
         {
                SAXParserFactory saxfact = SAXParserFactory.newInstance();
                saxfact.setNamespaceAware(true);
                //saxfact.setValidating(true);
                SAXParser parser = saxfact.newSAXParser() ;
                
                XMLReader reader = parser.getXMLReader();
    reader.setFeature("http://xml.org/sax/features/validation", true);
    reader.setFeature("http://apache.org/xml/features/validation/schema",true);
    //reader.setFeature ("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", true);
                reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "CorrespondenceMessageResponse.xsd");
                
                parser.parse(new InputSource(new StringReader(pStrXMLSring)),this);
         }
            catch (Exception saxe)
            {
             Renewalog.warn(saxe.getMessage());
            }

    Exception
    *****************
    cvc-elt.1: Cannot find the declaration of element 'ns1:correspondence-message-response'.

    Threaded Messages (6)

  2. Exception in schema validation[ Go to top ]

    Please post your schema.
  3. Scehma for the XML[ Go to top ]

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by Golden Stickwood (INSYSTEMS TECHNOLOGIES) -->
    <xs:schema targetNamespace="http://www.insystems.com/correspondence12" xmlns:tns="http://www.insystems.com/correspondence12" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="correspondence-message-response">
    <xs:annotation>
    <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
    <xs:sequence>
    <xs:element name="response" type="tns:correspondence-response" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:complexType name="correspondence-response">
    <xs:choice>
    <xs:element name="success">
    <xs:complexType>
    <xs:sequence>
    <xs:choice>
    <xs:element name="letter-generation-response">
    <xs:complexType>
    <xs:attribute name="letter-id" type="xs:string" use="required"/>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    <xs:element name="warnings" minOccurs="0">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="corr-warning" type="tns:info" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="failure">
    <xs:complexType>
    <xs:all>
    <xs:element name="errors">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="error" type="tns:info" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:all>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>
    <xs:complexType name="info">
    <xs:attribute name="code" type="xs:string" use="required"/>
    <xs:attribute name="description" type="xs:string" use="optional"/>
    </xs:complexType>
    </xs:schema>
  4. Scehma for the XML[ Go to top ]

    Try making elementFormDefault in the root element "unqualified". I think that should do it. I tested it with xmlspy
  5. Scehma for the XML[ Go to top ]

    also try adding attributeFormDefault="unqualified"
  6. Schema for the XML[ Go to top ]

    I still get error
    **************************
    org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns1:correspondence-message-response'.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at com.metlife.renewal.parser.RenewalParser.parseXML(RenewalParser.java:141)
    at com.metlife.renewal.parser.RenewalParser.main(RenewalParser.java:88)
  7. Exception in schema validation[ Go to top ]

    Implement your own error handler and give it to your parser. get more detailed message. You will solve your problem on your own.