【问题标题】:xsd - limit type to one of three custom typesxsd - 将类型限制为三种自定义类型之一
【发布时间】:2014-09-30 17:10:15
【问题描述】:

也许我的词汇有些错误:

我想创建一个 xsd,它为元素定义了许多“自定义类型”之一。
我的 xml 应该具有始终相同的“someInfo”的根,然后一个元素是一种 CustomTypeA 或 CustomTypeB。

XML 应该如下所示:

<xml>
  <someInfo>whatever is important</someInfo>
  <info type="CustomTypeA">
    <CustomTypeA-1>F1</CustomTypeA-1>
    <CustomTypeA-2>F2</CustomTypeA-2>
  </info>
</xml>

或者在其他情况下,如果是其他 CustomType,则 info 应该是:

<xml>
  <someInfo>whatever is important in an other case</someInfo>
  <info type="CustomTypeB">
    <CustomTypeB-1 attr1="someAttribute">F1</CustomTypeB-1>
  </info>
</xml>

【问题讨论】:

    标签: xml xsd restriction


    【解决方案1】:

    如果您希望元素的类型取决于其属性之一的值,您有两种选择:

    (a) 使用“xsi:type”作为属性名,其值为模式中类型的名称

    (b) 将 XSD 1.1 与条件类型分配的新功能一起使用。如果您能够使用 XSD 1.1,那是最好的解决方案,但并不是每个人都支持它。

    【讨论】:

    • 我现在用正常的xs:extension 内容解决了这个问题,但这有帮助。
    【解决方案2】:

    我昨天做了一些条件类型赋值here。更多关于条件类型赋值的信息可以阅读at the last example here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2012-09-14
      相关资源
      最近更新 更多