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

发布于 2024-12-08 05:18:29 字数 1997 浏览 0 评论 0原文

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

我正在使用 此工具 尝试根据 .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

我显然不明白某些事情。

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 something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-12-15 05:18:29

由于 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文