我正在尝试编写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'.
但据我所见,我已正确宣布。谁能指出我在这里做错了什么?感谢
答案 0 :(得分:2)
第一个问题实际上是这一行:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:bks="urn:books">
books
不像文档中的其他地方那样大写。
这是一个更“完整”的修复:
我做了几件事。首先,我删除了targetNamespace
和xmlns: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>
答案 1 :(得分:1)
我为你写了一个新的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>
答案 2 :(得分:1)
我认为您的XSD没有任何问题,但是使用XML。
您缺少XMl中的命名空间规范。
它应显示为:<Books xmlns="urn:books">