XSD:允许任何未知元素以任何顺序 [英] XSD: Allow any unknown element in any order

查看:26
本文介绍了XSD:允许任何未知元素以任何顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 XSD 架构.在这个架构中,一些元素是已知的和强制性的,其他元素是未知的和可选的:

I need to write an XSD schema. In this schema, some elements are known and mandatory, others are unknown and optional:

<father>
    <childMandatory1 />
    <childMandatory2 />
    <childOptionnal1 />
</father>

或:(更改强制子顺序)

or: (changing the mandatory children order)

<father>
    <childMandatory2 />
    <childMandatory1 />
</father>

我知道强制孩子(但不知道他们的顺序).但我不知道是否会有任何可选的孩子(人)(如果有,他们的名字).

I know the mandatory children (but not their order). But I don't know if there will be any optional child(ren) (and if so, their names).

我尝试使用xs:all",但xs:all"不允许使用any":

I tried with "xs:all", but "xs:all" does not allow "any" :

<xs:element name="father">
        <xs:complexType>
            <xs:all>
                <xs:element ref="childMandatory1" />
                <xs:element ref="childMandatory2" />
                <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/> 
        <!-- error here ! -->
                </xs:all>
            </xs:complexType>
    </xs:element>

我尝试了序列,但我应该知道元素的顺序.(我没有)

I tried with sequence, but I should know the order of elements. (and I don't)

我尝试了选择,但选择不能与任何和某些强制性元素一起使用.(并且我想确保必需元素存在)

I tried with choice, but choice cannot work with any and some mandatory elements. (and I want to be sure that the mandatory element are present)

推荐答案

根据您的要求,您已经达到了唯一粒子属性规则,该规则非常重要以至于 它有自己的维基百科页面.

With your requirement, you have hit the Unique Particle Attribution rule, which is important enough that it has its own Wikipedia page.

问题在于将任何顺序任何地方的任何元素结合起来.处理器无法确定某个元素属于哪个声明.childMandatory1 是属于 xs:any 还是属于元素声明?

The problem is with combining any order with any element anywhere. There is no way that a processor can deterministically determine to what declaration a certain element belongs to. Does childMandatory1 belong to xs:any or does it belong to the element declaration?

一旦您使用 xs:any,您就很容易遇到这个问题.这就是为什么允许序列与 xs:any 组合的原因,但前提是序列中的项是强制性且有序的,否则再次无法说明哪个元素属于哪个声明.

As soon as you use xs:any you can easily run into this issue. That is why a sequence is allowed to be combined with xs:any, but only if the items in the sequence are mandatory and in order, otherwise there is again no telling what element belongs to what declaration.

如果您创建此类 XSD 的原因是为了验证输入是否存在某些元素,您可以切换到 XSD 1.1,您可以在其中使用 xs:assert 解决此问题,或者您可以使用一种不同的工具,如 RelaxNg 或 Schematron,它们是替代的标准化 XML 模式语言.

If the reason you create such an XSD is to validate input for the presence of certain elements, you can switch to XSD 1.1, where you can resolve this using xs:assert, or you can use a different tool, like RelaxNg or Schematron, which are alternative standardized XML schema languages.

如果你想从中创建一个对象模型,你可能会走运,因为即使你设法这样做,(反)序列化器也无法告诉彼此的声明.

If you want to create an object model out of this, you may run out of luck, because even if you manage to do so, the (de)serializer will have no way telling the declarations from each other.

以下是 XSD 1.1 中的示例:

Here's an example in XSD 1.1:

<xs:complexType name="BookType">
    <xs:all>
        <xs:any maxOccurs="unbounded" namespace="##any" />
    </xs:all>
    <xs:assert test="self::author | self::title | self:isbn" xpathDefaultNamespace="urn:test" />
</xs:complexType>

一篇关于使用可变内容容器编写可扩展 XSD 架构的好文章 可以在 XFront 上找到.虽然很老,但它今天仍然非常适用.还有一篇关于使用 xs:any 的文章.

A good and helpful article on writing extensible XSD Schema's using Variable Content Containers can be found on XFront. While old, it still very much applies today. There's also an article on the use of xs:any.

这篇关于XSD:允许任何未知元素以任何顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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