【问题标题】:cvc-complex-type.3.2.2: Attribute xsi:schemaLocation is not allowed to appear in <people> in Java DOMcvc-complex-type.3.2.2:属性 xsi:schemaLocation 不允许出现在 Java DOM 的 <people> 中
【发布时间】:2013-05-05 22:19:05
【问题描述】:

我正在尝试使用 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">  

我该如何解决这个问题?

【问题讨论】:

  • 有类似的错误,也是由于http://www.w3.org/2001/XMLSchema-instanceurl 中的问题

标签: java xml dom jaxp saxparseexception


【解决方案1】:

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

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

到-

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

为我工作!

【讨论】:

    【解决方案2】:

    尝试替换此代码:

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

    【讨论】:

      【解决方案3】:

      问题出在这里:

      <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 Schema 文档、schemaLanguageschemaSource 属性中。

      【讨论】:

        猜你喜欢
        • 2014-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        • 2023-03-09
        • 2016-06-18
        • 2014-12-28
        • 1970-01-01
        相关资源
        最近更新 更多