XSD 验证 - 序列中的任何(“唯一粒子属性") [英] XSD validation - ANY in SEQUENCE ("Unique Particle Attribution")

查看:18
本文介绍了XSD 验证 - 序列中的任何(“唯一粒子属性")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XSD 架构:

I have the following XSD schema:

<xsd:schema xmlns="http://www.mynamespace.test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.mynamespace.test/" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:include schemaLocation="../Components.xsd"/>
    <xsd:element name="PO" type="POType"/>
    <xsd:complexType name="POType">
        <xsd:sequence>
            <xsd:element ref="PA" maxOccurs="unbounded"/>
            <xsd:element ref="PB" maxOccurs="unbounded"/>
            <xsd:any minOccurs="0" />
        </xsd:sequence>
        <xsd:attributeGroup ref="SomeAttrGroup"/>
        <xsd:attributeGroup ref="SomeOtherAttrGroup"/>
    </xsd:complexType>
</xsd:schema>

我基本上想确保我的 PO 元素包含 PA 元素和 PB 元素(PA 在 PB 之前),其中允许任何类型的元素位于 PA 之前、PA 和 PB 之间以及 PB 之后...我尝试在所有这些地方添加 xsd:any,但由于唯一粒子属性",即使只有其中一个也不可能.

Where I basically want to make sure that my PO element contains PA elements and PB elements (PA before PB), where there are any kind of elements allowed to be in front of PA, in between PA and PB and after PB... I tried adding xsd:any at all those places, but even only one of them is not possible because of "Unique Particle Attribution".

我理解为什么这会引发错误(无法区分现有 PB 元素是属于 ANY 部分还是序列中的实际 PB).但我看不出如何实现我真正想要的:这是否可能,以及如何实现?

I understand why this raises an error (can't tell the difference between an existing PB element to belong to the ANY part or the actual PB in the sequence). But I see no way of how to achieve what I actually want: is it possible at all, and how would it be done?

PS:ANY 元素可以与 PA 和 PB 元素位于同一命名空间中,但不能是 PA/PB 元素本身.

PS: The ANY elements can be in the same namespace as the PA and PB elements, just not the PA/PB elements itself.

推荐答案

事实证明这在 XSD 1.0 中是不可能的,我进一步研究了 XSD 1.1 的功能.

As it turned out this was not possible in XSD 1.0, I further investigated the features of XSD 1.1.

我要感谢 kjhughes 的回答(它有效,但在拥有大序列时不是很用户友好,...)并为我指明了正确的方向,但事实证明 XSD 1.1 有一些东西专为这种行为而设计:

I'd like to thank kjhughes for his answer (which works, but isn't very user friendly when having large sequences, ...) and pointing me in the right direction, but it turns out that XSD 1.1 has something especially designed for this kind of behaviour:

OpenContents(请参阅:XML Schema 1.1, Part 3: An Introduction to XML Schema 1.1 和这里的答案:如何忽略未知标签的验证 )

OpenContents (see: XML Schema 1.1, Part 3: An introduction to XML Schema 1.1 and this answer here: How to ignore the validation of unknown tags )

要允许未知元素,您可以在 complexType 上使用 Open Content.

To allow unknown elements, you can use an Open Content on your complexType.

<xsd:complexType name="CatalogEntry">
    <xsd:openContent mode="interleave">
        <xsd:any namespace="##any" processContents="skip"/>
    </xsd:openContent>

    <xsd:sequence>
        <xsd:element name="artist" type="xsd:string"/>
        <xsd:element name="album" type="xsd:string"/>
        <xsd:element name="price" type="xsd:decimal"/>
        <xsd:element name="release_date" type="xsd:dateTime"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>

指定模式 interleave 允许未知元素介于两者之间,或者 suffix 仅允许序列末尾的元素.

Specify mode interleave to allow unknown elements anywhere in between, or suffix to only allow elements at the end of the sequence.

还有为整个架构指定 defaultOpenContent 的选项:

There's also the option to specify a defaultOpenContent for a whole schema:

<xsd:schema ...>
    ...
    <xsd:defaultOpenContent mode="interleave">
        <xsd:any namespace="##any" processContents="skip"/>
    </xsd:defaultOpenContent>
    ...
    <xsd:complexType name="CatalogEntry">
        <xsd:sequence>
            <xsd:element name="artist" type="xsd:string"/>
            <xsd:element name="album" type="xsd:string"/>
            <xsd:element name="price" type="xsd:decimal"/>
            <xsd:element name="release_date" type="xsd:dateTime"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>
    <xsd:element name="cd" type="tns:CatalogEntry"/>
    ...
</xsd:schema>

这篇关于XSD 验证 - 序列中的任何(“唯一粒子属性")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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