XSD验证XML:找不到元素的声明

时间:2013-01-09 14:18:47

标签: xml xsd

我一直在通过xsd搞乱XML验证,但我还是新手。我尝试验证这个xml,它会弹出这个错误: cvc-elt.1:找不到元素'customers'的声明。 [5]

<?xml version="1.0"?>

<customers xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://w3schools.com requested_customer.xsd">

 <customer name="Vladimir Putin" address="St. Petersburg, wadim street 23, Russia"/>
</customers>

和XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xs:element name="customers">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="customer">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name"/>
                <xs:attribute type="xs:string" name="address"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

首先,您的XML不会将XML名称空间用于其内容,因此要在没有目标名称空间的情况下引用XSD的文件位置,您应该使用xsi:noNamespaceSchemaLocation属性。

其次,您必须确保XSD文件的位置为验证程序所知,并且可以通过验证程序访问。

您的XML / XSD组合完全有效。

答案 1 :(得分:1)

另外,如果您打算在customer内允许多个customers,则需要更改:

  <xs:element name="customer">

要:

  <xs:element name="customer" maxOccurs="unbounded">

当然,maxOccurs也可以是有限值,例如100maxOccurs(和minOccurs)都默认为1See)。 minOccurs也可以是0