全局元素定义可以被局部元素定义覆盖吗?

发布于 2024-12-12 01:34:13 字数 814 浏览 0 评论 0原文

在 XML 模式中定义全局元素,然后在特定父元素的上下文中覆盖该元素定义是否合法?同一元素是否可以在模式中全局和本地定义,还是必须是其中之一?

覆盖全局元素的示例可能如下所示。 Note 被定义为包含几个其他元素的全局元素。然而,在书籍的上下文中,它是一个仅包含字符串的元素。

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="book">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Is it legal to define a global element in XML schema and then override that element definition within the context of a particular parent element? Can the same element be defined both globally and locally within a schema or does it have to be one or the other?

An example of overriding a global element might look like the following. Note is defined as a global element containing several other elements. However, within the context of a book, it is an element that simply holds a string.

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="book">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

書生途 2024-12-19 01:34:13

是的,你可以这样做。如果您希望使用全局定义,则可以使用 ref 而不是 name。但是,请注意,不允许将这两者组合在同一序列中,因为验证器将无法再确定您指的是两者中的哪一个。

编辑:实际上,你并没有真正“压倒”任何东西。您刚刚对名为“note”的元素进行了匿名定义。如果您使用 ref 然后在本地更改定义,它将被覆盖,但据我所知,这也无法完成。无论如何这都没有意义。

Yes, you may do so. If you wish to use the global definition, you'd use ref instead of name. However, do mind that combining these two within the same sequence would not be allowed since a validator could then no longer determine which of the two you mean.

EDIT: actually, you're not really "overriding" anything. You've just made an anonymous definition of an element with name "note". It'd be overriding if you used ref and then changed the definition locally, but that can't be done either AFAIK. It wouldn't make sense anyway.

﹏半生如梦愿梦如真 2024-12-19 01:34:13

是的,您可以执行此操作,因为您没有覆盖任何内容。您有一个全局定义,而您的“内部”注释(在书中)是一个本地定义,实际上与全局注释无关。

Yes, you can do this because you haven't overridden anything. You have one global definition and your "inner" note (within book) is a local definition that is in fact unrelated to the global note.

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