2010-10-05 36 views
2

此XML documentation似乎是說,該ID派生類型支持的模式,但是當我嘗試定義一個與此代碼:
如何定義具有模式的XML模式ID屬性?

 <complexType name="CourseType"> 
      <attribute name="courseNumber" type="ID"> 
       <pattern value="[A-Z]{2}(\d{3}).(\d{3})" /> 
      </attribute> 
      <attribute name="numOfCredits" type="university:CourseCredits" /> 
      <element name="course_name" type="university:MixedName" /> 
      <element name="course_professor" type="string" /> 
     </complexType>> 


......我得到的氧氣XML錯誤編輯器說The content of 'courseNumber' must match (annotation?, (simpleType?)). A problem was found starting at: pattern.

我是否正確定義我的模式爲該ID屬性嗎?

回答

3

如果您需要限制內置的簡單數據類型,您應該創建自己的simpleType。使用Derivation by Restriction。嘗試是這樣的:

<simpleType name='better-ID'> 
    <restriction base='ID'> 
    <pattern value='(\d{3}).(\d{3})'/> 
    </restriction> 
</simpleType> 

<complexType name="CourseType"> 
     ... 
     <attribute name="courseNumber" type="better-ID"/> 
     <attribute name="numOfCredits" type="university:CourseCredits" /> 
</complexType> 

或者你也可以嵌入simpleType

<complexType name="CourseType"> 
      ... 
      <attribute name="courseNumber"> 
       <simpleType> 
        <restriction base='ID'> 
        <pattern value='(\d{3}).(\d{3})'/> 
        </restriction> 
       </simpleType> 
      </attribute> 
      <attribute name="numOfCredits" type="university:CourseCredits" /> 
    </complexType> 

參見@jasso下面的評論來修復您的XSD一些其他錯誤。

+2

這個想法是正確的,但這個解決方案有一些問題。 'complexType'不能包含'element'作爲直接的孩子。所有元素都必須是單個「all」,「choice」,「group」或「sequence」元素的後代。還有屬性需要在元素容器之後定義*。 – jasso 2010-10-05 12:06:30

+2

@kchau,@Shcheklein:由OP指定的ID模式似乎也很奇怪。 IIRC \ d表示十進制數,因此它可以包含一個點。因此該ID僅包含數字和1-3個點。但它應該是一個有效的'xs:ID',但'ID'需要是有效的NCNames,所以它們不能以數字開頭!要麼我錯了,要麼在作業中有一個「bug」。 – jasso 2010-10-05 12:08:22

+0

@jasso:感謝您的指正。我無法修復我的答案中的代碼,因爲我不知道@ kchau究竟想要什麼 - 序列,組,選擇。然而,我的答案確定了你的意見。 – Shcheklein 2010-10-05 14:03:37