如何将 xsd:sequence 与 xsd:all 混合 [英] how to mix xsd:sequence with xsd:all

查看:42
本文介绍了如何将 xsd:sequence 与 xsd:all 混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要要求前 n 个元素按顺序排列,然后是几个可以按任意顺序排列的元素.

I need to require the first n elements to be in a sequence followed by several elements that may be in any order.

例如,假设我有这个 xml:

For example, lets' say I have this xml:

<root>
    <person>
        <fn>First name</fn>
        <ln>Last Name</ln>
        <gender>F</gender>
        <age>33</age>
        <middleName>Middle</middleName>
    </person>
</root>

我需要要求有 2 个第一个元素,并且必须是按照这个确切的顺序,后跟 3 个元素,并且这 3 个可以是任意顺序.

I need to require that has 2 first elements and which must be in that exact order, followed by 3 elements , and and these 3 can be in any order.

我试过了,但它似乎不是模式的有效 xml:

I tried this but it does not seem like it's a valid xml for schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="person">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="fn" minOccurs = "1" maxOccurs="1" type="xsd:string"/>
                <xsd:element name="ln" minOccurs = "1" maxOccurs="1" type="xsd:string"/>
            </xsd:sequence>

            <xsd:all>
                <xsd:element name="gender" minOccurs = "1" maxOccurs="1"/>
                <xsd:element name="age" minOccurs = "1" maxOccurs="1"/>
                <xsd:element name="middleName" minOccurs = "1" maxOccurs="1"/>
            </xsd:all>

        </xsd:complexType>
    </xsd:element>

</xsd:schema>

推荐答案

不能直接做.

您可以将 xsd:all 替换为 xsd:choice,它允许三个元素可能有 8 种排列,这对于三个元素来说几乎是可管理的,但很快就会变得笨拙.

You could replace the xsd:all with an xsd:choice that allows the possible 8 permutations of the three elements, which is just about manageable for three but gets unwieldy quite rapidly.

在 XSD 1.1 中,我建议使用 xsd:all 内容模型,然后使用断言对其进行限制.

In XSD 1.1 I would suggest using an xsd:all content model and then restricting it with an assertion.

然而,我也想问问为什么你想以这种方式设计你的文档.在 XSD 不允许您做某事的地方,通常是因为 XSD 的设计者认为这不是一件非常明智的事情.

However, I would also question why you want to design your document this way. Where XSD doesn't allow you to do something, it's often because the designers of XSD didn't think it was a very smart thing to do.

这篇关于如何将 xsd:sequence 与 xsd:all 混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆