No declaration found for element

旧城冷巷雨未停 提交于 2020-01-16 16:48:13

问题


I have a simple XML and XSD files. I am using Xerces to generate .h/cpp files but when I run the application it gives an error:

no declaration found for element 'x:books'

My XML file is:

<?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>

and XSD file is:

<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>

I have done a simpler demo with Xerces but this one uses namespaces and I think that seems to causing the trouble.


回答1:


Change the namespace on the root element of your XML file (urn:books) to match the targetNamespace (urn:BookStore) of your 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">

and then your XML will validate against your XSD.



来源:https://stackoverflow.com/questions/34818957/no-declaration-found-for-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!