Java – error: invalid content found starting from element ‘x’ Expected ‘{x}’

I'm trying to validate simple XML with simple XSD, but I always get this error:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'linux'. One of '{linux}' is expected.

Why? The tag 'Linux' was found and is one of {Linux}!

Java code:

public static void main(String[] args) {
    try {
        InputStream xml = new FileInputStream("data/test.xml");
        InputStream xsd = new FileInputStream("data/test.xsd");

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));

        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xml));

        log.info("OK!");
    } catch (Exception e) {
        log.error(":(");
        log.error(e.getMessage());
    }
}

Data / test xml:

<?xml version="1.0" encoding="utf-8"?>
<so xmlns="http://test/">
    <linux>
        <debian>true</debian>
        <fedora>true</fedora>
    </linux>
</so>

Data / test xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="so">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="linux">
                    <xs:complexType>
                        <xs:sequence minOccurs="1" maxOccurs="unbounded">
                            <xs:any processContents="lax" maxOccurs="unbounded"/>
                        </xs:sequence></xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Solution

Because the pattern does not specify elementformdefault = "qualified", the local element declaration of element "Linux" declares that there is no element in the namespace, but the instance has a Linux element in the namespace "http: / / test /" The error message is confusing because it does not know if the problem is namespace related

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>