2015-04-05 77 views
0

我试图用ID类型属性限制两种类型的工会:XSD:定义的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>

所以基本思想是属性值应该与枚举{from/to/cc/bcc/subject/sendTime}或模式“attachment \ d + -metadata”匹配。

当使用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元素上指定类型,所以我不知道如何指定联合的结果也是ID类型。我也曾尝试:

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

但限制不允许工会它们下面。这甚至有可能吗?

回答

0

I am trying to restrict an attribute with type ID to a union of two types:

一般两种类型的一些类型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 

为什么它不被视为一个有效的限制,我不能想到一个很好的理由:可能只是一个监督的规范。但事情就是这样。

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处理器,那么您可能会获得更好的结果。

+0

谢谢!无论如何,我会使用你的模式,因为它更容易阅读。它看起来像XSD 1.1在这方面没有帮助,虽然我不能确定,因为我不认为JAXB支持1.1。 – 2015-04-06 05:42:09