xml 找不到元素“赋值”的声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449797/
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
Cannot find the declaration of element 'assignments'
提问by Levi
So I am beginning with XML and Schemas and I ran across this today and I have not been able to figure it out.
所以我从 XML 和 Schemas 开始,今天我遇到了这个问题,但我一直无法弄清楚。
I am getting and error that says,
我得到和错误说,
Ln 5 Col 2 : Cannot find the declaration of element 'assignments'.
Ln 5 Col 2:找不到元素“赋值”的声明。
I believe I have declared the element, but perhaps I am missing something and have not.
我相信我已经声明了元素,但也许我错过了一些东西并且没有。
This is my XML file:
这是我的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<assignments
xmlns="http://www.w3.org/2001/XMLSchema-instance"
SchemaLocation="A3.xsd"
>
<assignment id="a1">
<name>Schemas</name>
<page>110</page>
</assignment>
<assignment id="a2">
<name>Namespaces</name>
<page>258</page>
<files>names.xml</files>
<files>names.dtd</files>
</assignment>
<assignment id="a3">
<name>RELAX NG</name>
<page>305</page>
<files>account.xml</files>
<files>customers.xml</files>
<files>finance.xsd</files>
</assignment>
</assignments>
This is my Schema file:
这是我的架构文件:
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.leviHymanson.net/web340/ns"
targetNamespace="http://www.leviHymanson.net/web340/ns" elementFormDefault="qualified"
>
<element name="assignments" type="target:TypeAssignments"></element>
<complexType name="TypeAssignments">
<sequence>
<element name="assignment" type="target:assignmentInfo"></element>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"></element>
<element name="page" type="target:TypePage"></element>
<element name="file" type="target:TypeFile" minOccurs="0" maxOccurs="unbounded"></element>
</sequence>
</complexType>
<simpleType name="TypePage">
<restriction base="integer">
<minInclusive value="50" />
<maxInclusive value="498" />
</restriction>
</simpleType>
<simpleType name="TypeFile">
<restriction base="string">
<enumeration value=".xml" />
<enumeration value=".dtd" />
<enumeration value=".xsd" />
</restriction>
</simpleType>
</schema>
As I am still learning, feel free to point out any other mistakes I may have made not related to the problem.
由于我仍在学习中,请随时指出我可能犯的与问题无关的任何其他错误。
Thanks
Levi
谢谢
列维
采纳答案by Levi
The solution to this problem was that I was not declaring my main element 'assignments' as a complex element, I actually wasn't declaring it as anything at all.
这个问题的解决方案是我没有将我的主要元素“赋值”声明为一个复杂元素,实际上我根本没有将它声明为任何东西。
So by taking this line from my schema file:
因此,通过从我的架构文件中获取这一行:
<element name="assignment" type="target:assignmentInfo" minOccurs="0" maxOccurs="unbounded"></element>
and changing it into this:
并将其更改为:
<element name="assignments">
<complexType>
<sequence>
<element name="assignment" type="target:assignmentInfo" minOccurs="0" maxOccurs="unbounded"></element>
</sequence>
</complexType>
</element>
The element was properly defined, thanks for the help everyone.
元素已正确定义,感谢大家的帮助。
Levi
列维
回答by David Andres
Try the following. I took out some of the extra details of the schema element, but with a little tweaking something like the following should work:
请尝试以下操作。我去掉了 schema 元素的一些额外细节,但稍微调整一下,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="assignments" type="TypeAssignments" />
<xs:complexType name="TypeAssignments">
<xs:sequence>
<xs:element name="assignment" type="assignmentInfo"
minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="assignmentInfo">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="page" type="TypePage" />
<xs:element name="files" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
<xs:simpleType name="TypePage">
<xs:restriction base="xs:integer">
<xs:minInclusive value="50" />
<xs:maxInclusive value="498" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="TypeFile">
<xs:restriction base="xs:string">
<xs:enumeration value=".xml" />
<xs:enumeration value=".dtd" />
<xs:enumeration value=".xsd" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Notes:
注意事项:
- The ID attribute has been moved from
TypeAssignmentstoassignmentInfo. - All types and element names have been prefixed with the namespace
xs - I've added the minOccurs and maxOccurs attributes for the assignment elements. Otherwise, only a single assignment element is allowed within assignments.
- I've auto-closed your elements using "/>" instead of "" Not necessary, but a bit less typing to do.
- ID属性已经从移动
TypeAssignments到assignmentInfo。 - 所有类型和元素名称都以命名空间为前缀
xs - 我为赋值元素添加了 minOccurs 和 maxOccurs 属性。否则,赋值中只允许一个赋值元素。
- 我已经使用 "/>" 而不是 "" 自动关闭了你的元素 没有必要,但打字要少一些。
All in all, I didn't modify all that much so you have the right idea.
总而言之,我没有做太多修改,所以你有正确的想法。
回答by kdgregory
In your instance document, as excerpted above, you define the xmlnsattribute on the <assignments>element, and do not elsewhere define a namespace. This means that the namespace of <assignments>and all of its descendents is set to "http://www.w3.org/2001/XMLSchema-instance".
在您的实例文档中,如上面摘录的,您xmlns在<assignments>元素上定义属性,而不是在其他地方定义命名空间。这意味着名称空间<assignments>及其所有后代都设置为“ http://www.w3.org/2001/XMLSchema-instance”。
Your schema document, however, specifies a targetNamespacevalue of "http://www.leviHymanson.net/web340/ns". Since the elements in your instance document does not have this namespace, validation fails.
但是,您的架构文档指定了targetNamespace“ http://www.leviHymanson.net/web340/ns”的值。由于实例文档中的元素没有此命名空间,因此验证失败。
Start by changing your instance document to look like this:
首先将您的实例文档更改为如下所示:
<assignments
xmlns="http://www.leviHymanson.net/web340/ns"

