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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16495674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:08:56  来源:igfitidea点击:

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

javaxmldomjaxpsaxparseexception

提问by Little Child

I am attempting to validate my XML using XSD in Java by using DOM validator.
Although, manually, I know that the document is indeed valid, DOM validator shouts back at me and says:

我正在尝试使用 DOM 验证器在 Java 中使用 XSD 来验证我的 XML。
虽然我手动知道该文档确实有效,但 DOM 验证器对我大喊并说:

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

I have made sure that:
setNamespaceAware()is set to true
The schemaLanguageproperty was set before schemaSource
schemaLanguageis set to http://ww.w3.org/2001/XMLSchema
Both the XSDand XMLare in the same folder as the .javaand .classfile

我已经确定的是:
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

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

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">  

How do I resolve this?

我该如何解决?

采纳答案by Little Child

The problem is here:

问题在这里:

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

and

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

The problem is the cin the w3c.org. DOM parser expects it to be w3.orgeverywhere-- in the XML document, in the XML Schema Document, in the schemaLanguageand the schemaSourceproperties.

问题是.c 文件中的cw3c.org。DOM 解析器希望它w3.org无处不在——在 XML 文档中、在 XML 模式文档中、在schemaLanguageschemaSource属性中。

回答by Artem Tsushko

Try to replace this code:

尝试替换此代码:

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);

with this one:

有了这个:

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);

回答by Anagha

I faced similar problem. In your XML file, try changing the schemaLocation attribute -

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

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

to -

到 -

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

Worked for me!

为我工作!