How to fix the error: cvc-elt.1: Cannot find the declaration of element 'xsd:schema' without namespaces?

How to fix the error: cvc-elt.1: Cannot find the declaration of element 'xsd:schema' without namespaces?

我正在使用 input.xsd 架构文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="Mobiles">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Mobile" type="Mobile" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="Mobile">
        <xsd:sequence>
            <xsd:element name="Model" type="xsd:string"/>
            <xsd:element name="OS">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="Android"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="Origin" type="xsd:string"/>
            <xsd:element name="Material" type="xsd:string"/>
            <xsd:element name="Samsung" type="Samsung"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="Samsung" mixed="true">
        <xsd:sequence>
            <xsd:element name="Wlan">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="802.11"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="CardSlot" type="xsd:string"/>
            <xsd:element name="RadioAvailability" type="xsd:boolean" default="true"/>
            <xsd:element name="BluetoothAvailability" type="xsd:boolean" default="false"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

并且 input.xml 文件看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Mobiles
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="input.xsd">
    <Mobile>
        <Model>G975F</Model>
        <OS>Android</OS>
        <Origin>USA</Origin>
        <Material>Plastic</Material>
        <Samsung>
            <Wlan>802.11</Wlan>
            <CardSlot>MicroSD</CardSlot>
            <RadioAvailability>true</RadioAvailability>
            <BluetoothAvailability>true</BluetoothAvailability>
        </Samsung>
    </Mobile>
    <Mobile>
        <Model>G986</Model>
        <OS>Android</OS>
        <Origin>USA-Israel</Origin>
        <Material>Silicon-Plastic</Material>
        <Samsung>
            <Wlan>802.11</Wlan>
            <CardSlot>MicroSD</CardSlot>
            <RadioAvailability>true</RadioAvailability>
            <BluetoothAvailability>false</BluetoothAvailability>
        </Samsung>
    </Mobile>
    <Mobile>
        <Model>G770F</Model>
        <OS>Android</OS>
        <Origin>Israel</Origin>
        <Material>Silicon-Plastic</Material>
        <Samsung>
            <Wlan>802.11</Wlan>
            <CardSlot>MicroSD</CardSlot>
            <RadioAvailability>true</RadioAvailability>
            <BluetoothAvailability>false</BluetoothAvailability>
        </Samsung>
    </Mobile>
</Mobiles>

为了更好地理解理论,我遵循了这个 answer as example without namespaces for schema and this

但是当我尝试验证 xsd 架构文件时,我总是在控制台本地遇到问题:

cvc-elt.1: Cannot find the declaration of element 'xsd:schema'.
org.xml.sax.SAXParseException; systemId: file:///F:/IdeaProject/TestProject/input.xsd; lineNumber: 2; columnNumber: 58; cvc-elt.1: Cannot find the declaration of element 'xsd:schema'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1901)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:741)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
    at com.epam.rd.java.basic.practice7.controller.DOMController.parse(DOMController.java:96)
    at com.epam.rd.java.basic.practice7.controller.DOMController.main(DOMController.java:331)
    at com.epam.rd.java.basic.practice7.DOMControllerTest.main(DOMControllerTest.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:412)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:48)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.lang.Thread.run(Thread.java:748) 

使用逻辑进行验证,例如:

public void parse(boolean validate) throws ParserConfigurationException, SAXException, IOException {
            
    System.out.println("Invoking parse()");
    // obtain DOM parser
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    
    try{
        dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);

    } catch(ParserConfigurationException exp){
        logger.severe(MESSAGE);
        logger.severe(Arrays.toString(exp.getStackTrace()));
    }
    
    // set properties for Factory

    // XML document contains namespaces
    dbf.setNamespaceAware(true);

    // make parser validating
    if (validate) {
        // turn validation on
        dbf.setFeature(Constants.FEATURE_TURN_VALIDATION_ON, true);

        // turn on xsd validation
        dbf.setFeature(Constants.FEATURE_TURN_SCHEMA_VALIDATION_ON, true);
    }

    DocumentBuilder db = dbf.newDocumentBuilder();

    // set error handler
    db.setErrorHandler(new DefaultHandler() {
        @Override
        public void error(SAXParseException e) throws SAXException {
            // throw exception if XML document is NOT valid
            throw e;
        }
    });

    // parse XML document
    Document document = db.parse(xmlFileName);

    // get root element
    Element root = document.getDocumentElement();

    // create container
    mobiles = new Mobiles();

    // obtain mobiles nodes
    NodeList mobileNodes = root
            .getElementsByTagName(Names.MOBILE);

    // process mobiles nodes
    for (int j = 0; j < mobileNodes.getLength(); j++) {
        Mobile question = getMobile(mobileNodes.item(j));
        // add mobile to container
        mobiles.getMobileList().add(question);
    }
}

我检查过类似的问题,例如 ,但我没有不同的 uri httphttps

我也检查过这个 answer,但由于实施限制,我不能对 gradle 项目使用任何第三方依赖项(仅当它不是唯一的解决方案时)。

CoreFiling XML Schema Validator 报告关于 XML 架构和 XML 实例:

XML instance present.
XML schema present.

Well Formed: VALID
Schema Validation: VALID

如果需要,我可以提供更多信息。如果我做错了什么,有人可以纠正我吗?提前致谢。

错误,

cvc-elt.1: Cannot find the declaration of element xsd:schema without namespaces?

通常表示错误地尝试将 XSD 作为验证主题传递给验证器,而不是传递预期的 XML 文件。

在您的情况下,请确保 xmlFileName 用于 XML 文件,而不是 XSD。

另一方面,如果您真的想验证 XSD 本身,请参阅

  • Validating XSD itself
  • XML Schema to validate XML Schemas?