我用XSD到底哪里出了问题

本文关键字:问题 XSD 我用 | 更新日期: 2023-09-27 18:07:13

我想写一个XSD文档

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:bks="urn:books">
 <xsd:element name="Books" type="bks:BooksForm"/>
    <xsd:complexType name="BooksForm">
        <xsd:sequence>
            <xsd:element name="Book"    type="bks:BookForm"  minOccurs="0"     maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="Author"   type="bks:AuthorForm" minOccurs="1"  /><!--1-->
      <xsd:element name="Isbn"    type="xsd:string" minOccurs="1" />
      <xsd:element name="Title"    type="xsd:string" minOccurs="1" />
      <xsd:element name="Publisher" type="xsd:string"  minOccurs="1" />
      <xsd:element name="YearValue" type="bks:YearForm" minOccurs="1" /><!--2-->
    </xsd:sequence>
    <xsd:attribute name="Cover"   type="xsd:string"/>
  </xsd:complexType>
    <xsd:complexType name="AuthorForm">
    <xsd:sequence>
     <xsd:element name="Name"   type="bks:NameForm"/><!--1-->
      <xsd:element name="Contact"    type="bks:ContactForm"/> <!--2-->
    </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="YearForm">
        <xsd:sequence>
            <xsd:element name="Year"   type="xsd:integer"/><!--1-->
        </xsd:sequence>
    <xsd:attribute name="Edition"   type="xsd:integer"/>
    </xsd:complexType>
    <xsd:complexType name="NameForm">
    <xsd:sequence>
     <xsd:element name="First"   type="xsd:string"/><!--1-->
      <xsd:element name="Last"    type="xsd:string"/> <!--2-->
    </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="ContactForm">
    <xsd:sequence>
     <xsd:element name="Phone"   type="xsd:string"/><!--1-->
     </xsd:sequence>
     <xsd:attribute name="Office"   type="xsd:string"/>
    </xsd:complexType>
</xsd:schema>
与以下XML文档 匹配的

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Courses.xsl"?>
<Books>
  <Book Cover="Paper back">
    <Isbn>978-1-4652-0558-2</Isbn>
    <Title>Service-Oriented Computing</Title>
    <Author>
      <Name>
        <First>John</First>
        <Last>Doe</Last>
      </Name>
      <Contact Office="BY400">
        <Phone>480-965 000</Phone>
      </Contact>
    </Author>
    <Publisher>Kendall Hunt</Publisher>
    <Year Edition="3">
      <Year>2012</Year>
    </Year>
  </Book>
....</Books>

然而,当我尝试在线验证它时,我得到以下错误

Line 3, 8   cvc-elt.1.a: Cannot find the declaration of element 'Books'.

但在我看来,我的声明是正确的。有人能指出我在这里做错了什么吗?由于

我用XSD到底哪里出了问题

第一个问题实际上是这一行:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:bks="urn:books">

books在这里不像文档中其他地方那样大写。

这是一个更"完整"的修复:

我做了几件事。首先,我删除了targetNamespacexmlns:bks部分。然后我重新订购了BookForm类型中的一些项目,将YearValue固定为Year,因为它在xml数据中。然后我删除了所有对bks的引用。它现在验证。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="Books" type="BooksForm"/>
    <xsd:complexType name="BooksForm">
        <xsd:sequence>
            <xsd:element name="Book"    type="BookForm"  minOccurs="0"     maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="Isbn"    type="xsd:string" minOccurs="1" />
      <xsd:element name="Title"    type="xsd:string" minOccurs="1" />
      <xsd:element name="Author"   type="AuthorForm" minOccurs="1"  /><!--1-->
      <xsd:element name="Publisher" type="xsd:string"  minOccurs="1" />
      <xsd:element name="Year" type="YearForm" minOccurs="1" /><!--2-->
    </xsd:sequence>
    <xsd:attribute name="Cover"   type="xsd:string"/>
  </xsd:complexType>
    <xsd:complexType name="AuthorForm">
    <xsd:sequence>
     <xsd:element name="Name"   type="NameForm"/><!--1-->
      <xsd:element name="Contact"    type="ContactForm"/> <!--2-->
    </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="YearForm">
        <xsd:sequence>
            <xsd:element name="Year"   type="xsd:integer"/><!--1-->
        </xsd:sequence>
    <xsd:attribute name="Edition"   type="xsd:integer"/>
    </xsd:complexType>
    <xsd:complexType name="NameForm">
    <xsd:sequence>
     <xsd:element name="First"   type="xsd:string"/><!--1-->
      <xsd:element name="Last"    type="xsd:string"/> <!--2-->
    </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="ContactForm">
    <xsd:sequence>
     <xsd:element name="Phone"   type="xsd:string"/><!--1-->
     </xsd:sequence>
     <xsd:attribute name="Office"   type="xsd:string"/>
    </xsd:complexType>
</xsd:schema>

我为你写了一个新的XSD;希望它能满足你的需求:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Books">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Book">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="Isbn"/>
              <xs:element type="xs:string" name="Title"/>
              <xs:element name="Author">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Name">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:string" name="First"/>
                          <xs:element type="xs:string" name="Last"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="Contact">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:string" name="Phone"/>
                        </xs:sequence>
                        <xs:attribute type="xs:string" name="Office"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element type="xs:string" name="Publisher"/>
              <xs:element name="Year">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:short" name="Year"/>
                  </xs:sequence>
                  <xs:attribute type="xs:byte" name="Edition"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:string" name="Cover"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我看不出XSD有什么问题,但XML有问题。您在XMl中缺少名称空间规范。它应该是:<Books xmlns="urn:books">