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

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

我构建了以下 ('Person.xsd') XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:person.com.test"
           xmlns="urn:person.com.test">
    <xs:element name="person" type="Person" />
    <xs:complexType name="Person">
                    <xs:sequence>
                        <xs:element name="first_name" type="xs:string" />
                        <xs:element name="last_name" type="xs:string"/>
                    </xs:sequence>
    </xs:complexType>   
</xs:schema>

以及以下 XML 文档 ('Person.xml'):

<?xml version="1.0"?>
<person 
    xmlns="urn:person.com.test"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:person.com.test person.xsd" >
    <first_name>Joe</first_name>
        <last_name>Bloggs</last_name>
</person>

但是当我验证 XML 时(我使用的是 Netbeans 8.x ,但我尝试过的其他验证器给出了非常相似的结果);我收到以下无用的消息:

XML validation started.
Checking file:[...]/validator/src/main/resources/person.xml...
Referenced entity at "file:[...]/validator/src/main/resources/person.xsd".
cvc-complex-type.2.4.a: Invalid content was found starting with element 'first_name'. One of '{first_name}' is expected. [7] 
XML validation finished.

编辑:原来我在这里对 'targetnamespace' 和其他东西的含义有一些误解。

接受的答案有效 - 但@Ian Roberts 指出(这可能是另一个 post 实际上的真正重复) 'first_name' 和 'last_name' 是( 'person' 元素的子元素)仍然(出于某种原因)被视为根本不在命名空间中。

无论如何:我已经像这样修改了 XML 和 XSD - 这有效 - 我相信(这是我需要的)元素都在 person.com.test 命名空间现在在这里:

<?xml version="1.0"?>
<p:person 
    xmlns:p="urn:person.com.test"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:person.com.test person.xsd" >
    <p:first_name>Joe</p:first_name>
        <p:last_name>Bloggs</p:last_name>
</p:person>

这实际上也有效:(原来的XML)

<person 
    xmlns="urn:person.com.test"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:person.com.test person.xsd" >
    <first_name>Joe</first_name>
        <last_name>Bloggs</last_name>
</person>

只要 XSD 中有 elementFormDefault="qualified" 指令。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:person.com.test"
           xmlns="urn:person.com.test"
           elementFormDefault="qualified">

    <xs:element name="person" type="Person" />

    <xs:complexType name="Person">
        <xs:sequence>
                        <xs:element name="first_name" type="xs:string" />
                        <xs:element name="last_name" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>   
</xs:schema>

声明命名空间的前缀xmlns:prefix="urn:person.com.test"

<?xml version="1.0"?>
<prefix:person  xmlns:prefix="urn:person.com.test"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:person.com.test person.xsd" >
    <first_name>Joe</first_name>
    <last_name>Bloggs</last_name>
</prefix:person>

我通过 XMLSpear 验证