2011-10-03 86 views
2

我正在嘗試學習XML,並且我已經想出了一個模式和示例文件示例,以瞭解在我走之前是否知道自己在做什麼。根據我的自定義模式驗證XML文件

.xsd文件

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Assumption" type="assumptionType"/> 
    <xsd:complexType name="assumptionType"> 
     <xsd:sequence> 
      <xsd:element name="entries" type="entriesType"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:complexType name="entriesType"> 
     <xsd:sequence> 
      <xsd:element name="entry"> 
       <xsd:complexType> 
        <xsd:sequence> 
         <xsd:element name="x" type="xsd:decimal"/> 
         <xsd:element name="y" type="xsd:decimal"/> 
         <xsd:element name="value" type="xsd:decimal"/> 
        </xsd:sequence> 
       </xsd:complexType> 
      </xsd:element> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:schema> 

.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<Assumption> 
    <entries> 
     <entry> 
      <x>12</x> 
      <y>14</y> 
      <value>16</value> 
     </entry> 
     <entry> 
      <x>12</x> 
      <y>24</y> 
      <value>5</value> 
     </entry> 
    </entries> 
</Assumption> 

我使用this tool嘗試驗證對的.xsd將.xml。我收到以下錯誤:

The following errors were found: 
TYPE    LOC  MESSAGE 
Validation  9, 10  cvc-complex-type.2.4.d: Invalid content was found starting 
          with element 'entry'. No child element is expected at this point 

我顯然不理解的東西。

回答

3

由於entry是多個節點,需要指定maxOccurs="unbounded"

你的XSD是: -

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="Assumption" type="assumptionType"/> 
    <xsd:complexType name="assumptionType"> 
     <xsd:sequence> 
      <xsd:element name="entries" type="entriesType"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:complexType name="entriesType"> 
     <xsd:sequence> 
      <xsd:element name="entry" maxOccurs="unbounded"> 
       <xsd:complexType> 
        <xsd:sequence> 
         <xsd:element name="x" type="xsd:decimal"/> 
         <xsd:element name="y" type="xsd:decimal"/> 
         <xsd:element name="value" type="xsd:decimal"/> 
        </xsd:sequence> 
       </xsd:complexType> 
      </xsd:element> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:schema>