【问题标题】:Where to add a version to an XSD schema?在哪里将版本添加到 XSD 架构?
【发布时间】:2011-01-09 11:16:43
【问题描述】:

我处理的应用程序具有符合 XSD 模式的 XML 输出。随着功能被添加到应用程序中,XSD 发生变化,我想在 XSD 文件中注明架构的版本。

也许我遗漏了一些东西,但我还没有找到一种内置方法来标记架构的版本。

你是怎么做到的?

【问题讨论】:

标签: xml xsd version


【解决方案1】:

您可以使用 xsd 文档的命名空间

<xs:schema targetNamespace="http://yourcompany/yourapp/1.0" ... >
  ...
</xs:schema>

以 w3.org 定义的 xsd 为例,他们就是这样做的。请注意,更改这里的版本号通常对于您的 xsd 的任何消费者来说都是一个重大更改(无论实际更改有多小,或者您更改了版本号的哪一部分)。

对于影响较小的版本控制,似乎已就在根元素中放置版本属性达成一致:

<xs:schema version="1.0.0" ...>
  ...
</xs:schema>

【讨论】:

  • 我认为这是更改主要版本时的合适解决方案。但是,如果您想要进行更正或新的开发迭代,它就不是那么好,因为所有 XML 文档都必须迁移到新的名称空间。我在我的文章中指出的架构元素的版本属性似乎更适合 IMO 的那些情况。
  • 错误答案。见recommendations。你也可以试试this
  • 来自xml-google-styleguide: Namespaces MUST NOT be changed unless the semantics of particular elements or attributes has changed in drastically incompatible ways. [Rationale: Changing the namespace requires changing all client code.]
  • 有没有办法从根目录获取版本?即
【解决方案2】:

根据schema元素本身有一个version属性:

<schema
  attributeFormDefault = (qualified | unqualified) : unqualified
  blockDefault = (#all | List of (extension | restriction | substitution))  : ''
  elementFormDefault = (qualified | unqualified) : unqualified
  finalDefault = (#all | List of (extension | restriction | list | union))  : ''
  id = ID
  targetNamespace = anyURI
  version = token
  xml:lang = language
  {any attributes with non-schema namespace . . .}>
  Content: ((include | import | redefine | annotation)*, (((simpleType | complexType | group | attributeGroup) | element | attribute | notation), annotation*)*) 
</schema>

请参阅http://www.w3.org/TR/xmlschema-1/#key-schema,“3.15.2 XML 模式表示”

但是,如果您发布了架构,那么我认为处理它的最佳方法是使用目标命名空间。这将强制使用正确的版本(但会破坏兼容性)

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 2011-12-18
    • 2016-05-28
    • 2018-08-04
    • 2019-08-15
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多