Validating an XML file against my custom schema
I am trying to learn XML, and I have come up with a schema and sample file example to see if I know what I'm doing before I get to far.
.xsd file
<?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 file
<?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>
I am using this tool to try to validate the .xml against the .xsd. I am getting the following error:
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
I'm obviously not understanding somethin开发者_StackOverflow中文版g.
Since entry
is multiple node, you need to specify maxOccurs="unbounded"
Your XSD would be:-
<?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>
精彩评论