我有一个关于 XML 类型定义和值枚举的问题,特别是: 是否可以在枚举部分引用一个包含类型可能值的附加文件? 我的问题是,我必须定义具有数百个可能值的类型,而且可能值的数量会动态变化。
最佳答案
Is it possible to reference an additional file which holds the possible values for a type, within an enumeration section?
从技术上来说,是的。正如 @fly_ua 指出的,您可以使用 xs:import
或 xs:include
将其他 XML 架构文件导入/包含到您的架构中。
其他选项是使用 XML 实体或类似 XInclude 的内容,但我不建议这样做,因为它可能会给 XML 架构处理器带来麻烦。
My problem is, that I have to define types which have hundreds of possible values and moreover the number of possible values change dynamically.
在这种情况下,我强烈建议不要使用 xs:enumeration
。如果值是固定的并且提前已知,则应使用枚举。这是架构的一部分,因此如果更改这些值,则必须更改架构。
您正在寻找某种简单的动态方式。这样您就不必在值更改时更改架构,“只需更改外部文件”。
不存在“简单的动态方法”。
如果这些值是架构的一部分,则修复它们并将它们添加到架构中。这很简单,但不是动态的。
如果这些值必须是动态的,那么您要么必须在每次发生更改时发布新版本的架构,要么它们不是架构的一部分。
这是 GML 3.2.1 中的示例。他们定义了一个名为 CodeType
的类型:
<complexType name="CodeType">
<annotation>
<documentation>gml:CodeType is a generalized type to be used for a term, keyword or name.
It adds a XML attribute codeSpace to a term, where the value of the codeSpace attribute (if present) shall indicate a dictionary, thesaurus, classification scheme, authority, or pattern for the term.</documentation>
</annotation>
<simpleContent>
<extension base="string">
<attribute name="codeSpace" type="anyURI"/>
</extension>
</simpleContent>
</complexType>
这是一种简单的内容类型,带有附加的 codeSpace
属性。该代码空间逻辑z标识可以在这种情况下使用的一组代码。从技术上讲,这可能是某个字典文件的 URl 或众所周知的代码列表的 ID。
仅将枚举列表置于架构外部并随意更改它将会破坏使用 XML 实例的应用程序,除非它们遵循架构中的每一个更改。这可能相当困难。
关于另一个文件中可能值的 XML 模式枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27419874/