根据我的自定义架构验证XML文件

时间:2011-10-03 19:24:14

标签: xml xsd

我正在尝试学习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

我显然不理解某事。

1 个答案:

答案 0 :(得分: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>