如何在 XML Schema 中建模变体数据结构 [英] How to model variant data structures in XML Schema
问题描述
如何在不使用 xsi:type
的情况下对 XML 模式中的变体数据结构进行建模?我的目标是为名为
的元素定义架构,其属性和属性类型取决于 datatype
属性;例如:
How would you model a variant data structure in XML Schema, without using xsi:type
?
My goal is to define a schema for an element named <property>
whose attributes and attribute types depend on the datatype
attribute; for example:
<property datatype="integer" default="1234"/>
<property datatype="string" default="Abcde" maxlength="255"/>
此处,整数类型属性的default
属性应仅接受有效整数值,而字符串类型属性的default
属性应接受任何字符.
Here, the default
attribute of the integer-typed property shall only accept valid integer values, whereas the default
attribute of the string-typed property shall accept any characters.
此外,仅当 datatype="string"
时才允许使用 maxlength
属性.
Furthermore, the maxlength
attribute shall be allowed only if datatype="string"
.
即以下两个属性都应该发生架构验证错误:
I.e. a schema validation error should occur on both of the following properties:
<property datatype="integer" default="NotAnInteger"/>
<property datatype="integer" default="12345" maxlength="100"/> <!-- maxlength not allowed -->
我知道如果我使用 xsi:type
而不是 datatype
,这将很容易在 XML 模式中建模.然而,至少有两个原因,为什么我不想在这里使用 xsi:type
:
I know that if I would use xsi:type
instead of datatype
, this would be easy to model in XML Schema.
There are, however, at least two reasons, why I do not want to use xsi:type
here:
我在很多地方都读到过使用
xsi:type
不被认为是好的风格,例如xsi:type是邪恶的 或 聪明但没用,第 3 部分.
I have read in many places that it is not considered good style to use
xsi:type
, e.g. xsi:type is Evil or Smart but useless, part 3.
名称 xsi:type
根本没有足够准确地表达属性的用途.我更喜欢 datatype
.
The name xsi:type
simply does not express the purpose of the attribute precisely enough.
I rather would prefer datatype
.
推荐答案
您可以使用 XSD 1.1 中的条件类型分配来完成此操作,该功能在 Xerces 和 Saxon 的当前版本中实现.它不能在 XSD 1.0 中完成 - 这是一个众所周知的限制(通常在标签共现约束"下提及).
You can do this using Conditional Type Assignment in XSD 1.1, which is implemented in current releases of Xerces and Saxon. It can't be done in XSD 1.0 - it's a well known restriction (often referred to under the label "co-occurrence constraints").
这篇关于如何在 XML Schema 中建模变体数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!