2010-06-17 73 views
1

我需要定義一個XML模式。結果應該如下所示:XML模式 - 如何定義包含字符串或子元素的元素

<option value="priority">4</option> 
<option value="values"> 
    <value name="x86-32" lang="en-GB" group="root">x86 32-Bit</value> 
    <value name="x86-64" lang="en-GB" group="root">x86 64-Bit</value> 
    <value name="ARM" lang="en-GB" group="root">ARM</value> 
    <value name="PowerPC" lang="en-GB" group="root">PowerPC</value> 
    <value name="SPARC" lang="en-GB" group="root">SPARC</value> 
    <value name="PA-RISC" lang="en-GB" group="root">PA-RISC</value> 
    <value name="DEC-Alpha" lang="en-GB" group="root">DEC Alpha</value> 
</option> 
<option value="editable">true</option> 

所以元素「option」包含一個字符串或一組帶子元素的字符串。 我已經試過這樣的事情:

<xs:element name="options" minOccurs="0" maxOccurs="1"> 
    <xs:complexType mixed="true"> 
     <xs:sequence> 
      <xs:element name="option" maxOccurs="unbounded"> 
       <xs:complexType> 
        <xs:simpleContent> 
         <xs:extension base="xs:string">        
          <xs:attribute name="value" use="required" type="xs:string" /> 
         </xs:extension> 
        </xs:simpleContent> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element>      

但驗證不允許這樣的定義:

CVC-複雜type.2.2:元素 '選項' 必須沒有元素[兒童]和 該值必須有效。

任何想法如何解決它?

最好的問候, 拉狄克

+0

您是否特別需要將節點命名爲「選項」和「值」? – AllenG 2010-06-17 16:05:48

+0

不一定。 – 2010-06-17 16:25:33

回答

1

好吧,我想我已經找到可以接受的(對我來說)解決方案:

<xs:element name="options" minOccurs="0" maxOccurs="1"> 
<xs:complexType> 
    <xs:choice maxOccurs="unbounded">                  
     <xs:element name="option" maxOccurs="unbounded"> 
       <xs:complexType> 
       <xs:simpleContent> 
        <xs:extension base="xs:string">        
         <xs:attribute name="attribute" use="required" type="xs:string" /> 
        </xs:extension> 
       </xs:simpleContent> 
       </xs:complexType> 
     </xs:element> 
     <xs:element name="values" maxOccurs="unbounded"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="value" maxOccurs="unbounded"> 
         <xs:complexType> 
          <xs:simpleContent> 
           <xs:extension base="xs:string"> 
            <xs:anyAttribute/> 
           </xs:extension> 
          </xs:simpleContent> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
       <xs:attribute name="attribute" use="required" type="xs:string" /> 
       </xs:complexType> 
     </xs:element>                  
    </xs:choice> 
</xs:complexType> 

這使我創造這樣的XML文件:

<option attribute="priority">4</option> 
<values attribute="fieldOptions"> 
    <value name="x86-32" lang="en-GB" group="root">x86 32-Bit</value> 
    <value name="x86-64" lang="en-GB" group="root">x86 64-Bit</value> 
    <value name="ARM" lang="en-GB" group="root">ARM</value> 
    <value name="PowerPC" lang="en-GB" group="root">PowerPC</value> 
    <value name="SPARC" lang="en-GB" group="root">SPARC</value> 
    <value name="PA-RISC" lang="en-GB" group="root">PA-RISC</value> 
    <value name="DEC-Alpha" lang="en-GB" group="root">DEC Alpha</value> 
</values> 
<option attribute="editable">true</option> 

感謝AllenG的建議:)

相關問題