XSD 架构命名空间问题
如何使用 XSD“期望”元素具有命名空间?此时,我收到一条错误消息:“发现以元素 'ns:person' 开头的无效内容。需要 '{"":person}' 之一。"
如您所见,我定义了xs:element
上的 name
属性:“person”。如果我将“ns:person”定义为name
,则会收到一条错误消息,指出该名称无效 aaarghh。
我在这里缺少什么?
这是 XSD (ns.xsd):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="peoples">
<xs:complexType>
<xs:sequence>
<xs:element name="person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这是 XML (peoples.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<peoples xmlns:ns="url" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ns.xsd">
<ns:person>
<name>John</name>
</ns:person>
</peoples>
How do I 'expect' an element to have a namespace using XSD? At this point I receive an error saying: "Invalid content was found starting with element 'ns:person'. One of '{"":person}' is expected."
As you can see I define the name
attribute on the xs:element
: "person". If I define "ns:person" as the name
then I get an error that the name is invalid aaarghh.
What am I missing here?
This is the XSD (ns.xsd):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="peoples">
<xs:complexType>
<xs:sequence>
<xs:element name="person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This is the XML (peoples.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<peoples xmlns:ns="url" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ns.xsd">
<ns:person>
<name>John</name>
</ns:person>
</peoples>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要包含完整的 XSD 来帮助您。但从我看到的错误以及您在 XML 实例文档中包含“xsi:noNamespaceSchemaLocation”的事实来看,我看到的问题是您在定义架构时指定了 no targetNamespace 但您符合 <带有命名空间前缀的strong>person元素。只需删除实例文档中所有位置的“ns:”即可。
You need to include the complete XSD to help you. But from the error I see and the fact that you have included "xsi:noNamespaceSchemaLocation" in your XML instance document, the problem I see is that you defined your schema with no targetNamespace specified but you are qualifying person element with a namespace prefix. Just remove "ns:" at all places in your instance document and you are all good.