我有一个简单的 XML 和 XSD 文件。我正在使用 Xerces 生成 .h/cpp 文件,但是当我运行该应用程序时,出现错误:
未找到元素“x:books”的声明
我的 XML 文件是:
<?xml version="1.0"?>
<x:books xmlns:x="urn:books"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:BookStore books.xsd">
<book id="bk001">
<author>Writer</author>
<title>The First Book</title>
<genre>Fiction</genre>
<price>44.95</price>
<pub_date>2000-10-01</pub_date>
<review>An amazing story of nothing.</review>
</book>
<book id="bk002">
<author>Poet</author>
<title>The Poet's First Poem</title>
<genre>Poem</genre>
<price>24.95</price>
<pub_date>2001-10-01</pub_date>
<review>Least poetic poems.</review>
</book>
</x:books>
XSD 文件是:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:BookStore"
xmlns:bks="urn:BookStore">
<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="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
我用 Xerces 做了一个更简单的演示,但是这个演示使用了命名空间,我认为这似乎造成了麻烦。
更改 XML 文件根元素上的命名空间 (urn:books
)来匹配targetNamespace
(urn:BookStore
) 你的 XSD...
Change
<x:books xmlns:x="urn:books"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:BookStore books.xsd">
to
<x:books xmlns:x="urn:BookStore"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:BookStore books.xsd">
然后您的 XML 将根据您的 XSD 进行验证。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)