java - cvc-elt.1.a : Cannot find the declaration of element 'people'

标签 java xml validation xsd

我正在尝试使用 XSD 验证我的 XML 文档。我尝试为所有内容提供 namespace ,包括默认 namespace 。但是,错误仍然存​​在。如果有人可以告诉我出了什么问题,我们将不胜感激

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- <!DOCTYPE people SYSTEM "validator.dtd"> -->

    <people 
    xmlns:cmuq="http://www.cmu.edu/ns/students"
    xmlns="http://www.cmu.edy/ns/blank"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
    xsi:schemaLocation="student.xsd"
    >
        <cmuq:student>
            <name>John</name>
            <course>Computer Technology</course>
            <semester>6</semester>
            <scheme>E</scheme>
        </cmuq:student>

        <cmuq:student>
            <name>Foo</name>
            <course>Industrial Electronics</course>
            <semester>6</semester>
            <scheme>E</scheme>
        </cmuq:student>
    </people>   

XSD

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cmu.edu/ns/blank"
targetNamespace="http://www.cmu.edu/ns/students">
    <xs:element name="people">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="student" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string" />
                            <xs:element name="course" type="xs:string" />
                            <xs:element name="semester">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="1" />
                                        <xs:enumeration value="2" />
                                        <xs:enumeration value="3" />
                                        <xs:enumeration value="4" />
                                        <xs:enumeration value="5" />
                                        <xs:enumeration value="6" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="scheme">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value = "E|C" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>  

请告诉我如何解决该错误

<小时/>

伊恩·罗伯茨回答后更新

XSD <schema>标签:

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.cmu.edu/ns/blank"
    targetNamespace="http://www.cmu.edu/ns/blank"
    elementFormDefault="qualified"> 

XML 的根,<people>标签。

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

它仍然没有验证。我现在放弃了添加 xmlns:cmuq 的想法但问题仍然存在。 >

最佳答案

您所拥有的架构在 http://www.cmu.edu/ns/students 命名空间(项目的目标命名空间)中声明了 people 元素。 schema)以及没有命名空间的所有嵌套元素(因为您不使用 elementFormDefault)。所以需要这样

<cmuq:people
  xmlns:cmuq="http://www.cmu.edu/ns/students"
  xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.cmu.edu/ns/students student.xsd"
>
    <student>

另请注意,xsi:schemaLocation 需要是 namespaceUri schema 对的列表,而不仅仅是单个架构地址 - 此属性可以将不同的架构与每个命名空间。

关于java - cvc-elt.1.a : Cannot find the declaration of element 'people' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477496/

相关文章:

java - spring中基于groups的动态POJO验证

java - JUnit 延迟断言消息创建

java - 数据源、XADataSource 和 JTA

java - 需要帮助解析 Twitter Oauth 的 XML 文件

c# - 为 Web 服务创建 XML 字符串

python - 带有消息检查的 Django/Python assertRaises

java - 将 XML 拆分为多个 xml 文件

java - 使用 Scanner 类 nextLine() 方法从文件中读取单词时出现问题

ios - 如何在 iOS 中将字符从一个位置修剪到另一个位置( Objective-C )?

javascript - 尽管 novalidate 和 false 返回,表单提交后仍重新加载页面