mavrav
mavrav

Reputation: 580

XML with namespace Validation with XSD throws exception

I have a xml file which is a response from Webservice.It has got various namespaces involved with it. When I try to validate it with appropriate XSD its throwing "org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'SOAP-ENV:Envelope'." The namespace declaration for all the namespaces are declared in the response. Following is my code

try {
        DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
            SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            SAXSource mainInputStream = new SAXSource(new InputSource(new FileInputStream(new File("FIXEDINCOME_v3_0.xsd"))));
            SAXSource importInputStream1 =new SAXSource(new InputSource(new FileInputStream(new File("Rating.xsd"))));
            SAXSource importInputStream2 = new SAXSource(new InputSource(new FileInputStream(new File("Datatypes.xsd"))));
            Source[] sourceSchema = new SAXSource[]{mainInputStream , importInputStream1, importInputStream2};
            Schema schema = schemaFactory.newSchema(sourceSchema);  
            xmlFact.setNamespaceAware(true);
            xmlFact.setSchema(schema);
            DocumentBuilder builder = xmlFact.newDocumentBuilder();
            xmlDOC = builder.parse(new InputSource(new StringReader(inputXML)));
            NamespaceContext ctx = new NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    String uri;
                    if (prefix.equals("ns0"))
                        uri = "http://namespace.worldnet.ml.com/EDS/Standards/Common/Service_v1_0/";
                    else if (prefix.equals("ns1"))
                        uri = "http://namespace.worldnet.ml.com/EDS/Product/Services/Get_Product_Data_Svc_v3_0/";
                    else if (prefix.equals("ns2"))
                        uri = "http://namespace.worldnet.ml.com/DataSOA/Product/Objects/FixedIncome/FixedIncome_v3_0/";
                    else if (prefix.equals("ns3")) {
                        uri = "http://namespace.worldnet.ml.com/DataSOA/Product/Objects/Rating/Rating_v2_0/";
                    } else if (prefix.equals("SOAP-ENV")) {
                        uri = "http://schemas.xmlsoap.org/soap-envelope/";
                    } else
                        uri = null;

                    return uri;
                }

                // Dummy implementation - not used!
                public Iterator getPrefixes(String val) {
                    return null;
                }

                // Dummy implemenation - not used!
                public String getPrefix(String uri) {
                    return null;
                }
            };

            XPathFactory xpathFact = XPathFactory.newInstance();
            xPath = xpathFact.newXPath();
            xPath.setNamespaceContext(ctx);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 0

Views: 4298

Answers (3)

jtahlborn
jtahlborn

Reputation: 53694

The set of schemas you are providing for validation does not include the soap schema. you can either include the soap schema in the schema collection, or, if you don't care about validating the soap wrapper, just grab the actual body content element and run your validation from there.

Upvotes: 0

Alex
Alex

Reputation: 13941

I don't think the problem is with detecting the namespace definition for the SOAP-ENV prefix. The validator needs the XSD file that defines elements used in that namespace in order to validate the SOAP-ENV:Envelope element, so you need to tell the validator where that schema is located.

I think the solution is either to add the following to your XML reponse:

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://schemas.xmlsoap.org/soap/envelope/"
  xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/      
           http://schemas.xmlsoap.org/soap/envelope/">

Or, go download that schema off the web, save it to your local filesystem as an XSD file, and add it to your sourceSchema array. The first method should be preferred as it leads to more portable code (and XML).

Upvotes: 1

user1054394
user1054394

Reputation:

Have you tried using the following URI for the SOAP-ENV?

http://schemas.xmlsoap.org/soap/envelope/

Upvotes: 0

Related Questions