加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 错误:从元素’X’开始发现无效内容.预计会有一个'{

发布时间:2020-12-15 05:01:45 所属栏目:Java 来源:网络整理
导读:我正在尝试使用简单的XSD验证简单的 XML,但始终会收到此错误: cvc-complex-type.2.4.a: Invalid content was found starting with element 'linux'. One of '{linux}' is expected. 为什么?标签’linux’被找到并且是{linux}之一! java代码: public stat
我正在尝试使用简单的XSD验证简单的 XML,但始终会收到此错误:

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

为什么?标签’linux’被找到并且是{linux}之一!

java代码:

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

数据/的test.xml:

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

数据/ 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>

解决方法

因为模式没有指定elementFormDefault =“qualified”,所以元素“linux”的本地元素声明声明了没有命名空间中的元素,但实例在命名空间“http:// test /”中有一个linux元素.错误消息令人困惑,因为它无法清楚问题是否与命名空间有关.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读