2015-04-05 76 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