找不到元素的声明 [英] No declaration found for element

查看:44
本文介绍了找不到元素的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 XML 和 XSD 文件.我正在使用 Xerces 生成 .h/cpp 文件,但是当我运行该应用程序时出现错误:

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:

未找到元素x:books"的声明

no declaration found for element 'x:books'

我的 XML 文件是:

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>

和 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 做了一个更简单的演示,但这个演示使用了命名空间,我认为这似乎会造成问题.

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

推荐答案

更改 XML 文件 (urn:books) 根元素上的命名空间以匹配 targetNamespace (urn:BookStore) 你的 XSD...

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

这篇关于找不到元素的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆