2012-12-18 121 views
3

如何在一個xsd中定義一個具有父元素的子元素?如何在一個xsd中定義一個在子元素中具有父元素的元素

下面是一個例子:

<score seq="1" type="test" numeric="23"> 
    <option lbound="0" ubound=""> 
     <score seq="1" type="app" numeric="575"> 
      <option lbound="" ubound="0">0</option> 
     </score> 
    </option> 
</score> 
+1

這是不可能來自一個實例的實例制定出要在模式強加什麼限制。例如,分數/選項對的嵌套級別是否允許,或者是否要將其限制爲一個嵌套級別? –

回答

0

下面是嵌套任何級別的選項...

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 
    <xs:element name="score"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element minOccurs="0" maxOccurs="unbounded" ref="option"/> 
       <xs:element minOccurs="0" maxOccurs="unbounded" ref="ignore"/> 
      </xs:sequence> 
      <xs:attribute name="numeric" use="optional" type="xs:integer"/> 
      <xs:attribute name="seq" use="required" type="xs:integer"/> 
      <xs:attribute name="type" use="required" type="xs:NCName"/> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="option"> 
     <xs:complexType mixed="true"> 
      <xs:sequence> 
       <xs:element minOccurs="0" maxOccurs="unbounded" ref="score"/> 
      </xs:sequence> 
      <xs:attribute name="id" use="optional"/> 
      <xs:attribute name="lbound" use="optional"/> 
      <xs:attribute name="ubound" use="optional"/> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="ignore"/> 
</xs:schema> 

這適用於這兩個例子你已經給出:

例1

<score seq="1" type="test" numeric="23"> 
    <option lbound="0" ubound=""> 
     <score seq="1" type="app" numeric="575"> 
      <option lbound="" ubound="0">0</option> 
     </score> 
    </option> 
</score> 

例2(從評論)

<score seq="1" type="IN"> 
    <option id="1">10</option> 
    <ignore/> 
</score> 
1

像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="score"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="option"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="score"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element name="option"> 
         <xs:complexType> 
         <xs:simpleContent> 
          <xs:extension base="xs:unsignedByte"> 
          <xs:attribute name="lbound" type="xs:string" use="required" /> 
          <xs:attribute name="ubound" type="xs:unsignedByte" use="required" /> 
          </xs:extension> 
         </xs:simpleContent> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
        <xs:attribute name="seq" type="xs:unsignedByte" use="required" /> 
        <xs:attribute name="type" type="xs:string" use="required" /> 
        <xs:attribute name="numeric" type="xs:unsignedShort" use="required" /> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      <xs:attribute name="lbound" type="xs:unsignedByte" use="required" /> 
      <xs:attribute name="ubound" type="xs:string" use="required" /> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
     <xs:attribute name="seq" type="xs:unsignedByte" use="required" /> 
     <xs:attribute name="type" type="xs:string" use="required" /> 
     <xs:attribute name="numeric" type="xs:unsignedByte" use="required" /> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
相關問題