XSD:使用Union定义ID类型的限制

时间:2015-04-05 11:41:04

标签: xml xsd jaxb

我正在尝试将类型为ID的属性限制为两种类型的联合:



<attribute name="metaDataID" use="required">
  <simpleType>
    <union>
      <simpleType>
        <restriction base="ID">
          <enumeration value="from" />
          <enumeration value="to" />
          <enumeration value="cc" />
          <enumeration value="bcc" />
          <enumeration value="subject" />
          <enumeration value="sendTime" />
        </restriction>
      </simpleType>
      <simpleType>
        <restriction base="ID">
          <pattern value="attachment\d+-metadata" />
        </restriction>
      </simpleType>
    </union>
  </simpleType>
</attribute>
&#13;
&#13;
&#13;

所以基本的想法是属性值应该与枚举{from / to / cc / bcc / subject / sendTime}或模式&#34; attachment \ d + -metadata&#34;匹配。

使用JAXB验证时出现以下错误:

The attribute use 'metaDataID' in this type has type 'null', which is not validly derived from 'ID', the type of the matching attribute use in the base type.

这对我有意义。 metaDataID的新定义不再具有类型ID。但是,我无法在simpleTypeunion元素上指定类型,因此我不知道如何指定union的结果也是ID类型。我也尝试过:

&#13;
&#13;
<attribute name="metaDataID" use="required">
  <simpleType>
    <restriction base="ID">
      <union>
        ...
      </union>
    </restriction>
  </simpleType>
</attribute>
&#13;
&#13;
&#13;

但限制不允许其下的工会。这甚至可能吗?

2 个答案:

答案 0 :(得分:1)

对于从ID派生的类型执行ID约束时,XSD 1.0有点模糊;我希望像这样的例子的处理器的行为从实现到实现各不相同。

如果您放弃了联合并使用更复杂的模式在单个限制步骤中派生了所需类型,则可能会获得更好的结果:

  <simpleType>
    <restriction base="ID">
      <pattern value="from" />
      <pattern value="to" />
      <pattern value="cc" />
      <pattern value="bcc" />
      <pattern value="subject" />
      <pattern value="sendTime" />
      <pattern value="attachment\d+-metadata" />
    </restriction>
  </simpleType>

XSD 1.1(我认为)更明确一点。因此,如果您可以使用XSD 1.1处理器,也可能会获得更好的结果。

答案 1 :(得分:0)

  

我试图将类型ID的属性限制为两种类型的联合:

一般来说,两种类型的联合都是某种类型T的限制本身并不被认为是对T的限制。这与T是xs:ID无关。

我尝试了这个架构:

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="u">
    <xs:union>
      <xs:simpleType>
        <xs:restriction base="xs:positiveInteger">
          <xs:enumeration value="1"/>
        </xs:restriction>
      </xs:simpleType>  
      <xs:simpleType>
        <xs:restriction base="xs:positiveInteger">
          <xs:enumeration value="2"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>

  <xs:element name="e" type="xs:positiveInteger"/>

  <xs:element name="f" type="u" substitutionGroup="e"/>

</xs:schema>

和撒克逊报道:

Error on line 21 of test.xsd:
  Element f cannot be in the substitution group of e. Type u is not validly derived from type xs:positiveInteger

我无法想出为什么它不被视为有效限制的充分理由:可能只是规范中的疏忽。但事情就是这样。