cvc-complex-type.3.2.2:属性xsi:schemaLocation不允许出现在Java DOM中的<people> </people>

时间:2013-05-11 09:27:57

标签: java xml dom jaxp saxparseexception

我试图通过使用DOM验证器在Java中使用XSD验证我的XML 虽然手动地,我知道该文档确实有效,但DOM验证器对我大声疾呼并说:

cvc-complex-type.3.2.2: Attribute <xsi:schemaLocation> is not allowed to appear in the element <people>  

我已经确定:
setNamespaceAware()设置为true
schemaLanguage属性设置在schemaSource之前 schemaLanguage设置为http://ww.w3.org/2001/XMLSchema
XSDXML都与.java.class文件位于同一文件夹中

SSCCE

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

public class DOMValidator {
    String xmlInstance = null;
    String xmlSchema = null;


    public static void main(String[] args){
        DOMValidator validator = new DOMValidator();
        validator.validateXML("student.xsd",
                              "helloWorld.xml");
    }

    public void validateXML(String xsd,String xml){
        xmlSchema = xsd;
        xmlInstance = xml;
        beginValidation();
    }

    public void beginValidation(){
        try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(true);

            factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                                 "http://www.w3.org/2001/XMLSchema");
            factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
                                 xmlSchema);

            ErrorHandler errorHandler = new ErrorHandler();

            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setErrorHandler(errorHandler);
            builder.parse(xmlInstance);

            if(errorHandler.errorOccured == true){
                System.out.println("Document is Invalid.");
                System.out.println(errorHandler.ex.getMessage());
            }else{
                System.out.println("Doument is valid");
            }

        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(SAXException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private class ErrorHandler extends DefaultHandler{
        public SAXParseException ex = null;
        public boolean errorOccured = false;

        @Override public void error(SAXParseException ex){
            this.ex = ex;
            errorOccured = true;
        }

        @Override public void fatalError(SAXParseException ex){
            this.ex = ex;
            errorOccured = true;
        }

        @Override public void warning(SAXParseException ex){
            this.ex = ex;
            errorOccured = true;
        }
    }
}  

XSD

<xs:schema xmlns:xs="http://www.w3c.org/2001/XMLSchema"
        xmlns="http://www.cmu.edu/ns/blank"
        targetNamespace="http://www.cmu.edu/ns/blank"
        elementFormDefault="qualified">  

XML

<people
    xmlns="http://www.cmu.edu/ns/blank"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">  

如何解决此问题?

3 个答案:

答案 0 :(得分:7)

问题在于:

<xs:schema xmlns:xs="http://www.w3c.org/2001/XMLSchema" .....>

<people
    xmlns="http://www.cmu.edu/ns/blank"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"....>  

问题是w3c.org中的 c 。 DOM解析器期望它在任何地方都是w3.org - 在XML文档,XML架构文档,schemaLanguageschemaSource属性中。

答案 1 :(得分:0)

尝试替换此代码:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                     "http://www.w3.org/2001/XMLSchema");
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
                     xmlSchema);

这一个:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false); // we will use schema instead of DTD
factory.setNamespaceAware(true);
SchemaFactory schemaFactory = SchemaFactory
                                  .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schema = schemaFactory.newSchema(new File(xmlSchema));
factory.setSchema(schema);

答案 2 :(得分:0)

我遇到了类似的问题。在XML文件中,尝试更改schemaLocation属性 -

<... xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">

到 -

<... xsi:noNamespaceSchemaLocation= "student.xsd">

为我工作!