给定的文档中未检测到语法约束(DTD或XML模式)。

3
这是一个xsd文件。
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/onetext"
        xmlns:tns="http://www.example.org/onetext" 
        elementFormDefault="qualified">
  <element name="edge">
    <complexType>
      <attribute name="x" type="float"></attribute>
      <attribute name="y" type="float"></attribute>
      <attribute name="height" type="float"></attribute>
      <attribute name="width" type="float"></attribute>
      <attribute name="xhref" type="string"></attribute>
      <attribute name="id" type="string"></attribute>
      <attribute name="isLocked" type="string"></attribute>
      <attribute name="rx" type="double"></attribute>
      <attribute name="ry" type="float"></attribute>
      <attribute name="rotation" type="float"></attribute>
    </complexType>
  </element>
</schema>

这是一个XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<edge xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.example.org/onetext"
      xmlns:web="http://www.example.org/onetext"
      xsi:schemaLocation="web_one/WebContent onetext.xsd"
      id="WebApp_ID" version="3.0">
  <image x="336" 
         y="52" 
         height="57.6" 
         width="57.6"
         xhref="/dccp_repository/dam/logo/images/accc_logo.jpg" 
         id="Image_3"
         isLocked="false" 
         rx="336" 
         ry="52" 
         rotation="0" />
</edge>

我遇到了这样的问题。

在 xml 文档中没有检测到语法限制(DTD 或 XML 模式)。

如果我像下面所示更改了 xml 文件的根节点:

<edge xmlns="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:web="web_one/WebContent/onetext.xsd"
      xsi:schemaLocation="web_one/WebContent  
                          web_one/WebContent/onetext.xsd"
      id="WebApp_ID" 
      version="3.0">

然后我遇到了这种错误

cvc-elt.1: 找不到元素“edge”的声明。

我对这个概念感到困惑。请帮忙解释一下。

2个回答

2
您展示的模式文档声明了一个名为edge的元素,它位于命名空间http://www.example.org/onetext中(schema元素上的targetNamespace属性的值)。到目前为止,一切都很好。
在您的第一个XML文档实例中,最外层的元素名为edge,它位于命名空间http://www.example.org/onetext中。这与模式文档中的声明一致。到目前为止,一切都很好。如果您调用XSD验证器并告诉它使用您的模式文档来查找http://www.example.com/onetext命名空间中元素的声明,那么一切都应该没问题,验证器应该能够验证您的文档。当它这样做时,它会告诉您edge元素被声明为没有子元素,因此文档实例中的edge元素无效,因为它有一个名为image(在命名空间http://www.example.org/onetext中)的子元素。验证器还可能提到模式在该命名空间中没有任何名为image的元素的声明。当您收到这些错误消息时,您当前的问题将得到解决,您可以开始解决下一个问题,即使您的模式和XML在您正在定义和使用的数据格式上达成一致。
但是目前这种情况并没有发生。为什么?在您的第一个XML实例中,最外层元素具有xsi:schemaLocation="web_one/WebContent onetext.xsd"。翻译成英语就是“你好,模式验证器。如果您正在寻找命名空间web_one/WebContent中元素的模式声明,则会在位置onetext.xsd处找到该命名空间的模式文档。祝您愉快!”(好吧,规范实际上并没有说什么愉快的话;那只是我的解释。)这里有几个问题:
  1. 字符串web_one/WebContent不是绝对URI,因此不适合作为命名空间名称。
  2. 文档实例中的元素都位于命名空间http://www.example.org/onetext中,您的xsi:schemaLocation属性未告诉模式验证器在哪里查找该命名空间的声明。
  3. 如果验证器实际读取了模式文档onetext.xsd,并且如果那是您向我们展示的模式文档,则验证器应该抱怨命名空间名称web_one/WebContent与模式文档的目标命名空间不匹配。
您收到的错误消息(未检测到文档的语法约束)反映了问题2:您的验证器没有找到与您实例文档相关的任何命名空间的声明。
要纠正这个问题,需要用类似于 xsi:schemaLocation = "http://www.example.org/onetext onetext.xsd" 的内容替换当前的 xsi:schemaLocation 属性值规范(假设文档 onetext.xsd 是您展示给我们的模式文档,并且它与被验证的文档实例在同一个目录中 - 否则,请使用正确构造的相对URI)。
您的第二个XML实例文档包含一个在命名空间 http://www.w3.org/2001/XMLSchema 中的 edge 文档。验证器不需要指向此命名空间的模式文档的指针:它是XSD命名空间,而XSD验证器通常将该命名空间的模式硬编码到其中。因此,其错误消息并不是找不到任何模式;相反,错误消息是在XSD命名空间中没有找到任何名为edge的元素声明。
对于第二个XML实例所做的更改,只有那些真正不了解XML命名空间或XML命名空间声明的人才会认为是合理的。因此,我建议您花费一两个小时学习XML中命名空间的工作原理以及解析器如何从您的文档中理解每个元素和属性应与哪个(命名空间名称,本地名称)对相关联。一旦您这样做了,许多与XSD模式相关的事情(特别是许多错误消息)将变得更加清晰。
祝你好运!

0
 This is how XML look

 <element>
 <complextype name="edge">
         <height>some words here</height>
         <width>some words here</width>
  </complextype>
 </element>

 Tips: the <element> is the root and does not need a.   name.
 <complextype> is the one u can add name to which is the Child.
 The <height> does not need much styling except  you want to use  DTD for your styling

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接