两个文件,在同一目录,
XML文件如下:
-----------------------------------------------------------------
<?xml version="1.0" ?>
<BOOKS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="zhcen:book books.xsd"
xmlns:X="zhcen:book">
<X:BOOK ID="1">
<X:TITLE>No Title</X:TITLE>
<X:AUTHOR>zhcen</X:AUTHOR>
<X:PUBLISH>CCOT</X:PUBLISH>
</X:BOOK>
<X:BOOK ID="2">
<X:TITLE>No Title</X:TITLE>
<X:AUTHOR>jerry</X:AUTHOR>
<X:PUBLISH>CCOT</X:PUBLISH>
</X:BOOK>
<X:BOOK ID="3">
<X:TITLE>No Title</X:TITLE>
<X:AUTHOR>chalis</X:AUTHOR>
<X:PUBLISH>CCOT</X:PUBLISH>
</X:BOOK>
</BOOKS>
--------------------------------------------------------------------XSD文件如下:
-----------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="zhcen:book"
xmlns:X="zhcen:book" 
elementFormDefault="qualified">

<xs:element name="BOOKS">
<xs:complexType>
<xs:sequence>
<xs:element name="BOOK">
<xs:complexType>
<xs:sequence>
<xs:element name="TITILE" type="xs:string" />
<xs:element name="AUTHOR" type="xs:string" />
<xs:element name="PUBLISH" type="xs:string" />
</xs:sequence>
<xs:attribute name="ID" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
------------------------------------
工具是Visual studio.net 2003
验证XML时不通过,输出信息如下:
F:\ZHCEN\XMLStudy\books.xml(1): Visual Studio 未能找到此文档的架构。验证只能确保这是一个格式正确的 XML 文档,而无法针对架构验证数据。
F:\ZHCEN\XMLStudy\books.xml(2): 未声明“BOOKS”元素。 , (2, 2)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(5): 未声明“zhcen:book:BOOK”元素。 , (5, 3)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(6): 未声明“zhcen:book:TITLE”元素。 , (6, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(7): 未声明“zhcen:book:AUTHOR”元素。 , (7, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(8): 未声明“zhcen:book:PUBLISH”元素。 , (8, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(10): 未声明“zhcen:book:BOOK”元素。 , (10, 3)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(11): 未声明“zhcen:book:TITLE”元素。 , (11, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(12): 未声明“zhcen:book:AUTHOR”元素。 , (12, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(13): 未声明“zhcen:book:PUBLISH”元素。 , (13, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(15): 未声明“zhcen:book:BOOK”元素。 , (15, 3)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(16): 未声明“zhcen:book:TITLE”元素。 , (16, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(17): 未声明“zhcen:book:AUTHOR”元素。 , (17, 4)处发生了错误。
F:\ZHCEN\XMLStudy\books.xml(18): 未声明“zhcen:book:PUBLISH”元素。 , (18, 4)处发生了错误。刚开始学习,高手指点!

解决方案 »

  1.   

    <?xml version="1.0"?>
    <X:BOOKS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="zhcen:book books.xsd" xmlns:X="zhcen:book">
    <BOOK ID="1">
    <TITLE> No   Title </TITLE>
    <AUTHOR> zhcen </AUTHOR>
    <PUBLISH> CCOT </PUBLISH>
    </BOOK>
    <BOOK ID="2">
    <TITLE> No   Title </TITLE>
    <AUTHOR> jerry </AUTHOR>
    <PUBLISH> CCOT </PUBLISH>
    </BOOK>
    <BOOK ID="3">
    <TITLE> No   Title </TITLE>
    <AUTHOR> chalis </AUTHOR>
    <PUBLISH> CCOT </PUBLISH>
    </BOOK>
    </X:BOOKS>
    <?xml version="1.0" encoding="utf-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  targetNamespace="zhcen:book" elementFormDefault="unqualified">
    <xsd:element name="BOOKS">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="BOOK" maxOccurs="unbounded">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="TITLE" type="xsd:string"/>
    <xsd:element name="AUTHOR" type="xsd:string"/>
    <xsd:element name="PUBLISH" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="ID" type="xsd:string"/>
    </xsd:complexType>
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>