XSD:不允许空元素

发布于 2024-09-07 08:14:35 字数 1950 浏览 1 评论 0原文

我有一个如下所示的 XML 架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" minOccurs="0" maxOccurs="1"/>
                <xs:element name="B" minOccurs="0" maxOccurs="1"/>
                <xs:element name="C" minOccurs="0" maxOccurs="32"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

根据此架构,以下内容是有效的:

<root xsi:noNamespaceSchemaLocation="MySchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</root>

但是,我想让上述 XML 无效。

更具体地说,我想要求: 1. 至少有一个子元素,可以是 ,或 ,以及 2. 最多有一个 子代,最多有一个 子代。

建议?


解决方案是:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:choice>
                <xs:sequence>
                    <xs:element name="A"/>
                    <xs:element name="B" minOccurs="0"/>
                    <xs:element name="C" minOccurs="0" maxOccurs="32"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="B"/>
                    <xs:element name="C" minOccurs="0" maxOccurs="32"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="C" minOccurs="1" maxOccurs="32"/>
                </xs:sequence>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

I have an XML Schema that looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" minOccurs="0" maxOccurs="1"/>
                <xs:element name="B" minOccurs="0" maxOccurs="1"/>
                <xs:element name="C" minOccurs="0" maxOccurs="32"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The following is valid according to this schema:

<root xsi:noNamespaceSchemaLocation="MySchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</root>

However, I'd like to make the above XML invalid.

More specifically, I'd like to require:
1. that the <root> have at least one child element, be it an <A>, a <B>, or a <C>, and
2. that the <root> have at most one <A> child, and at most one <B> child.

Suggestions?


Solution is:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:choice>
                <xs:sequence>
                    <xs:element name="A"/>
                    <xs:element name="B" minOccurs="0"/>
                    <xs:element name="C" minOccurs="0" maxOccurs="32"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="B"/>
                    <xs:element name="C" minOccurs="0" maxOccurs="32"/>
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="C" minOccurs="1" maxOccurs="32"/>
                </xs:sequence>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

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

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

发布评论

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

评论(2

贪恋 2024-09-14 08:14:35

只使用一个 xs:选择 A、B 或 C,然后分别使用 0 个或多个 xs 怎么样?

啊,根据您的编辑,它必须是 A、AB 或 B 的选择,后跟 0 到 32 个 C。是的?

How about using exactly one xs:choice of A, B, or C followed by 0 or more of each of them?

Ah, with your edit it would have to be something like a choice of A, AB, or B followed by 0 to 32 Cs. Yes?

幸福%小乖 2024-09-14 08:14:35

您是否尝试过将 minOccurrs=1 添加到 xs:sequence 中?

Have you tried adding minOccurrs=1 to the xs:sequence?

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