XSD 架构命名空间问题

发布于 2024-10-09 15:43:43 字数 971 浏览 0 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-10-16 15:43:43

您需要包含完整的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文