macoo
macoo

Reputation: 47

cvc-elt.1.a: Cannot find the declaration of element 'ns0:Root'

I've written a code in Java to validate an xml file against an xsd schema. Although online validators return a positive result, the validator in Java keeps on failing. Please find below the snippets from the xml/xsd files:

XML:

<?xml version = "1.0" encoding = "UTF-8"?>
<ns0:Root xmlns:ns0 = "URLA">
    <ns0:DigitalAsset>

... XSD:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns="URLA"
     targetNamespace="URLA"
     elementFormDefault="qualified"
     attributeFormDefault="unqualified">
    <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
    <xs:element name="Root">

Error: cvc-elt.1.a: Cannot find the declaration of element 'ns0:Root'

Java code:

@Test
    public void testXmlStructure(){
        try {
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
            Document document = parser.parse(new File("./resources/2449346_20151007_ProductTaxonomy.xml"));

            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            Source schemaFile = new StreamSource(new File("./resources/Schema.xsd"));
            Schema schema = factory.newSchema(schemaFile);

            Validator validator = schema.newValidator();

            validator.validate(new DOMSource(document));

            } catch (IOException e){    
                Assert.fail(e.getMessage());
            } catch(SAXException e1){
                    Assert.fail(e1.getMessage());
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                Assert.fail(e.getMessage());
            }

    }

Unfortunately the data is senstive. I will try to replace it with some dummy elements and post it.

Thanks a lot for a suggestion.

EDIT:

You know what? I have changed my code to what follows and it worked:

@Test
    public void testXmlStructure(){
        try {

            Source xmlFile = new StreamSource(new File("./resources/2449346_20151007_ProductTaxonomy.xml"));

            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            Source schemaFile = new StreamSource(new File("./resources/Schema.xsd"));
            Schema schema = factory.newSchema(schemaFile);

            Validator validator = schema.newValidator();

            validator.validate(xmlFile);

            } catch (IOException e){    
                Assert.fail(e.getMessage());
            } catch(SAXException e1){
                    Assert.fail(e1.getMessage());
            }
    }

Upvotes: 2

Views: 2308

Answers (1)

Michael Glavassevich
Michael Glavassevich

Reputation: 1040

One of the likely reasons that your first attempt didn't work is because you were using a DocumentBuilder that was not namespace-aware. By default, the parser created by the DocumentBuilderFactory will not be namespace-aware. You should always enable namespace support on the DocumentBuilderFactory before you create the DocumentBuilder.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();

Without namespace support the DOM built by the DocumentBuilder will contain DOM Level 1 nodes that are missing values for their localName and namespaceURI (see DOM FAQ). These are values that the XML Schema validator is going to try read from the DOM when searching for an element declaration.

Upvotes: 3

Related Questions