2016-01-15 125 views
0

我有一個簡單的XML和XSD文件。我使用的Xerces生成的.h/cpp文件,但是當我運行該應用程序它提供了一個錯誤:沒有爲元素找到聲明

no declaration found for element '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做了一個更簡單的演示,但是這個使用了命名空間,我認爲這似乎導致了麻煩。

回答

2

更改XML文件(urn:books)的根元素的命名空間來匹配targetNamespaceurn:BookStore)您的XSD的...

變化

<x:books xmlns:x="urn:books" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="urn:BookStore books.xsd"> 

<x:books xmlns:x="urn:BookStore" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="urn:BookStore books.xsd"> 

,然後您的XML將根據您的XSD進行驗證。

相關問題