XML Schema 内容模型不是确定性的

发布于 2025-01-05 23:59:23 字数 2834 浏览 0 评论 0原文

我在 xml 架构方面遇到问题。

首先,我想向您展示 xml 的可能情况:

1.

<arrivalDate>01.01.2012</arrivalDate>

2.

<arrivalDate>01.01.2012</arrivalDate>
<departureDate>04.01.2012</departureDate>

3.

<arrivalDate>01.01.2012</arrivalDate>
<presentee>
    <firstName>User</firstName>
</presentee>

4.

<arrivalDate>01.01.2012</arrivalDate>
<departureDate>04.01.2012</departureDate>
<presentee>
    <firstName>User</firstName>
</presentee>

5.

<arrivalDate>later</arrivalDate>
<presentee>
    <firstName>User</firstName>
</presentee>

6。

<arrivalDate>later</arrivalDate>
<departureDate>5</departureDate> <!-- 1 till 5 extra days -->
<presentee>
    <firstName>User</firstName>
</presentee>

仅当设置了presentee 时,情况5 和6 才可能出现。

现在我已经创建了一个模式来处理这个问题:

<xs:choice>
    <xs:sequence>
        <xs:choice>
            <xs:sequence>
                <xs:element name="arrivalDate" type="date" />
                <xs:element name="departureDate" type="date" minOccurs="0" maxOccurs="1" />
            </xs:sequence>
            <xs:sequence>
                <xs:element name="arrivalDate" type="xs:string" fixed="later" />
                <xs:element name="departureDate" minOccurs="0" maxOccurs="1">
                    <xs:simpleType>
                        <xs:restriction base="xs:integer">
                            <xs:minInclusive value="1" />
                            <xs:maxInclusive value="5" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:choice>
        <xs:element name="presentee" minOccurs="1" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="title" type="title"/>
                    <xs:element name="firstName" type="name" />
                    <xs:element name="lastName" type="name" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
    <xs:sequence>
        <xs:element name="arrivalDate" type="date" />
        <xs:element name="departureDate" type="date" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
</xs:choice>

我尝试了对结构的几次更改来处理问题,但我没有得到“好的”解决方案。

i have trouble with a xml schema.

First I want to show you the possible cases of xml:

1.

<arrivalDate>01.01.2012</arrivalDate>

2.

<arrivalDate>01.01.2012</arrivalDate>
<departureDate>04.01.2012</departureDate>

3.

<arrivalDate>01.01.2012</arrivalDate>
<presentee>
    <firstName>User</firstName>
</presentee>

4.

<arrivalDate>01.01.2012</arrivalDate>
<departureDate>04.01.2012</departureDate>
<presentee>
    <firstName>User</firstName>
</presentee>

5.

<arrivalDate>later</arrivalDate>
<presentee>
    <firstName>User</firstName>
</presentee>

6.

<arrivalDate>later</arrivalDate>
<departureDate>5</departureDate> <!-- 1 till 5 extra days -->
<presentee>
    <firstName>User</firstName>
</presentee>

The cases 5 and 6 are only possible if presentee is set.

Now I've created a schema for handle this:

<xs:choice>
    <xs:sequence>
        <xs:choice>
            <xs:sequence>
                <xs:element name="arrivalDate" type="date" />
                <xs:element name="departureDate" type="date" minOccurs="0" maxOccurs="1" />
            </xs:sequence>
            <xs:sequence>
                <xs:element name="arrivalDate" type="xs:string" fixed="later" />
                <xs:element name="departureDate" minOccurs="0" maxOccurs="1">
                    <xs:simpleType>
                        <xs:restriction base="xs:integer">
                            <xs:minInclusive value="1" />
                            <xs:maxInclusive value="5" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:choice>
        <xs:element name="presentee" minOccurs="1" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="title" type="title"/>
                    <xs:element name="firstName" type="name" />
                    <xs:element name="lastName" type="name" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
    <xs:sequence>
        <xs:element name="arrivalDate" type="date" />
        <xs:element name="departureDate" type="date" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
</xs:choice>

I tried several changes to the structure, to handel the problem, but im don't get a "nice" solution.

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

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

发布评论

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

评论(2

不即不离 2025-01-12 23:59:23

您的 XML 架构是非确定性的,因为当将其应用到 XML 文档时,它无法决定您指的是arrivalDate和departmentDate的定义中的哪一个。您的思维方式是,第一个 指令让您可以在这些元素的一组定义(后跟演示者元素)或不带演示者元素的另一个定义之间进行选择。不幸的是,XML Schema 不执行某些前瞻操作。

解析后,XML 事件将一一呈现给验证器。因此,arrivalDate 被赋予您的模式,现在它必须知道该元素是否有效,但是......它无法弄清楚这些到达日期定义中的哪一个适用。如果它可以将这个决定推迟到稍后解析后续元素时,那么它可以,就像它如何通过丢失某些子元素来决定元素无效一样。但考虑到它们是分开的,这是不可能的。

W3C XML Schema 在以下几个方面存在缺陷:无序内容和跨节点限制。正如 Kevin 所建议的,XML Schema 1.1 可能对此有答案。我没用过它。其他要检查的选项是 RelaxNG,它比 XML Schema 更灵活、更强大,或者 Schematron,它允许进行比结构和简单类型检查更深入的验证。

The reason your XML schema is non-deterministic, is that when applying it to an XML document it can't decide which of the definitions for arrivalDate and departureDate you mean. You manner of thinking is that the first <choice> directive gives you a choice between either one set of definitions for these elements, followed by a presentee element, or another definition without the presentee element. Unfortunately XML Schema does not perform some look-ahead.

Upon parsing, the XML events are presented to a validator one by one. So the arrivalDate is given to your schema and now it has to know whether the element is valid or not, but... it can't figure out which of these arrival date definitions apply. If it could defer that decision to later when it has parsed subsequent elements it could, like how it might decide an element is invalid by missing certain child elements. But given that they're separate, this isn't possible in this way.

W3C XML Schema is bad at a couple of things: unordered content and cross-node restrictions. Like Kevin suggested, XML Schema 1.1 might have an answer to that. I haven't worked with it. Other options to check are RelaxNG, which is more flexible and powerful than XML Schema, or Schematron, which allows for exactly this sort of validation that needs to go deeper than structure and simple type-checks.

无边思念无边月 2025-01-12 23:59:23

这不是 XSD 1.0 可以处理的。 XSD 模型是确定性的,正如您所意识到的,您的语言不是确定性的。

您可以使用 union 来获得可以是日期或某些特定字符串值的类型,但 XSD 中没有任何内容可以让您指定当到达/离开不仅仅是日期时必须存在后面的元素。

我不知道 XSD 1.1 是否适合您,但它有断言,这可能会对您有所帮助。

This is not something XSD 1.0 can handle. XSD models are deterministic and as you are realizing, your language is not deterministic.

You could use union to have a type that could be either a date or some certain string values, but there is nothing in XSD to let you specify that a later element must be present when arrival/departure is not just a date.

I don't know whether XSD 1.1 is an option for you, but it has assertions, which might help you.

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