XML 架构验证:找不到元素的声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15642046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:56:58  来源:igfitidea点击:

XML Schema Validation : Cannot find the declaration of element

xmlxsdxml-validationxml-error

提问by Nerdio

I am still a bit new to XML Schema etc. and have been working to develop some XML, Schema and a Stylesheet (XSLT). I have made reasonable progress, but then realized that my Schema had stopped working, so I have taken it back to a simpler non-descript example.

我对 XML Schema 等还是有点陌生​​,并且一直致力于开发一些 XML、Schema 和样式表 (XSLT)。我已经取得了合理的进展,但后来意识到我的 Schema 已经停止工作,所以我把它带回了一个更简单的非描述性示例。

Here is my XML:

这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="Test.Namespace"  
      schemaLocation="http://myNameSpace.com Test1.xsd">
    <element1 id="001">
        <element2 id="001.1">
             <element3 id="001.1" />
        </element2>
    </element1>
</Root>

I have written a Schema that is here:

我写了一个架构在这里:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="Test.Namespace"
            elementFormDefault="qualified">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="element1" maxOccurs="unbounded" type="element1Type"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="element1Type">
        <xsd:sequence>
            <xsd:element name="element2" maxOccurs="unbounded" type="element2Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element2Type">
        <xsd:sequence>
            <xsd:element name="item" type="element3Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element3Type">
         <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>        
 </xsd:schema>

The Schema is representative of the structure of my real XML.

Schema 代表了我真正的 XML 的结构。

Now, when I try to validate my XML, I get this error:

现在,当我尝试验证 XML 时,出现此错误:

cvc-elt.1: Cannot find the declaration of element 'Root'. [7]

cvc-elt.1: Cannot find the declaration of element 'Root'. [7]

This I think is to do with a namespace issue on the Root element, but I am really not sure.

我认为这与 Root 元素上的命名空间问题有关,但我真的不确定。

Can someone suggest what I am doing wrong please.

有人可以建议我做错了什么。

采纳答案by Ian Roberts

cvc-elt.1: Cannot find the declaration of element 'Root'. [7]

cvc-elt.1:找不到元素“Root”的声明。[7]

Your schemaLocationattribute on the root element should be xsi:schemaLocation, and you need to fix it to use the right namespace.

schemaLocation在根元素上的属性应该是xsi:schemaLocation,您需要修复它以使用正确的命名空间。

You should probably change the targetNamespaceof the schema and the xmlnsof the document to http://myNameSpace.com(since namespaces are supposed to be valid URIs, which Test.Namespaceisn't, though urn:Test.Namespacewould be ok). Once you do that it should find the schema. The point is that all three of the schema's target namespace, the document's namespace, and the namespace for which you're giving the schema location must be the same.

您可能应该targetNamespace将架构的 和xmlns文档的 更改为http://myNameSpace.com(因为命名空间应该是有效的 URI,但Test.Namespace实际上不是,不过urn:Test.Namespace也可以)。一旦你这样做,它应该找到模式。关键是模式的目标名称空间、文档的名称空间和您为其提供模式位置的名称空间这三个必须相同。

(though it still won't validate as your <element2>contains an <element3>in the document where the schema expects item)

(尽管它仍然不会验证,因为您在架构期望的文档中<element2>包含)<element3>item

回答by Nerdio

Thanks to everyone above, but this is now fixed. For the benefit of others the most significant error was in aligning the three namespaces as suggested by Ian.

感谢上面的每个人,但现在已经解决了。为了他人的利益,最大的错误是按照 Ian 的建议对齐三个命名空间。

For completeness, here is the corrected XML and XSD

为了完整起见,这里是更正的 XML 和 XSD

Here is the XML, with the typos corrected (sorry for any confusion caused by tardiness)

这是 XML,更正了拼写错误(对于因迟到造成的任何混淆,我们深表歉意)

<?xml version="1.0" encoding="UTF-8"?>

<Root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="urn:Test.Namespace"  
    xsi:schemaLocation="urn:Test.Namespace Test1.xsd"
    >
    <element1 id="001">
        <element2 id="001.1">
            <element3 id="001.1" />
        </element2>
    </element1>
</Root>

and, here is the Schema

并且,这是架构

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:Test.Namespace"
            xmlns="urn:Test.Namespace"
            elementFormDefault="qualified">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="element1" maxOccurs="unbounded" type="element1Type"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="element1Type">
        <xsd:sequence>
            <xsd:element name="element2" maxOccurs="unbounded" type="element2Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element2Type">
        <xsd:sequence>
            <xsd:element name="element3" type="element3Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element3Type">
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>        
</xsd:schema>

Thanks again to everyone, I hope this is of use to somebody else in the future.

再次感谢大家,我希望这对未来的其他人有用。

回答by Petru Gardea

The targetNamespaceof your XML Schema does not match the namespace of the Rootelement (dot in Test.Namespacevs. comma in Test,Namespace)

targetNamespace你的XML模式的不匹配的命名空间的根元素(点在Test.Namespace主场迎战逗号Test,Namespace

Once you make the above agree, you have to consider that your element2has an attribute orderthat is not in your XSD.

一旦您同意上述内容,您必须考虑您的 XSD 中没有element2的属性顺序