XSD 验证 - 序列中的任何 ("Unique Particle Attribution")
XSD validation - ANY in SEQUENCE ("Unique Particle Attribution")
我有以下 XSD 架构:
<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 之间以及之后的任何类型的元素铅...
我尝试在所有这些地方添加 xsd:any,但由于 "Unique Particle Attribution".
,即使只有其中一个也是不可能的
我明白为什么这会引发错误(无法区分现有 PB 元素属于 ANY 部分还是属于序列中的实际 PB)。但我看不出如何实现我真正想要的:是否可能,如何实现?
PS:ANY 元素可以与 PA 和 PB 元素位于相同的命名空间中,只是不能与 PA/PB 元素本身。
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...
您不能在 XSD 1.0 中执行此操作,但您可以在 XSD 1.1 中执行此操作,因为它放宽了唯一粒子属性要求:
<xsd:schema xmlns="http://www.mynamespace.test/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mynamespace.test/"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xsd:include schemaLocation="../Components.xsd"/>
<xsd:element name="PO" type="POType"/>
<xsd:complexType name="POType">
<xsd:sequence>
<xsd:any minOccurs="0" />
<xsd:element ref="PA" maxOccurs="unbounded"/>
<xsd:any minOccurs="0" />
<xsd:element ref="PB" maxOccurs="unbounded"/>
<xsd:any minOccurs="0" />
</xsd:sequence>
<xsd:attributeGroup ref="SomeAttrGroup"/>
<xsd:attributeGroup ref="SomeOtherAttrGroup"/>
</xsd:complexType>
</xsd:schema>
事实证明这在 XSD 1.0 中是不可能的,我进一步研究了 XSD 1.1 的特性。
我想感谢 kjhughes 的回答(这个答案有效,但在有大序列时不是很友好,...)并为我指明了正确的方向,但事实证明 XSD 1.1 有专门为这种行为设计的东西:
OpenContents(参见: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
。
<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
仅允许序列末尾的元素。
还有为整个模式指定 defaultOpenContent
的选项:
<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 架构:
<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 之间以及之后的任何类型的元素铅... 我尝试在所有这些地方添加 xsd:any,但由于 "Unique Particle Attribution".
,即使只有其中一个也是不可能的我明白为什么这会引发错误(无法区分现有 PB 元素属于 ANY 部分还是属于序列中的实际 PB)。但我看不出如何实现我真正想要的:是否可能,如何实现?
PS:ANY 元素可以与 PA 和 PB 元素位于相同的命名空间中,只是不能与 PA/PB 元素本身。
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...
您不能在 XSD 1.0 中执行此操作,但您可以在 XSD 1.1 中执行此操作,因为它放宽了唯一粒子属性要求:
<xsd:schema xmlns="http://www.mynamespace.test/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mynamespace.test/"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xsd:include schemaLocation="../Components.xsd"/>
<xsd:element name="PO" type="POType"/>
<xsd:complexType name="POType">
<xsd:sequence>
<xsd:any minOccurs="0" />
<xsd:element ref="PA" maxOccurs="unbounded"/>
<xsd:any minOccurs="0" />
<xsd:element ref="PB" maxOccurs="unbounded"/>
<xsd:any minOccurs="0" />
</xsd:sequence>
<xsd:attributeGroup ref="SomeAttrGroup"/>
<xsd:attributeGroup ref="SomeOtherAttrGroup"/>
</xsd:complexType>
</xsd:schema>
事实证明这在 XSD 1.0 中是不可能的,我进一步研究了 XSD 1.1 的特性。
我想感谢 kjhughes 的回答(这个答案有效,但在有大序列时不是很友好,...)并为我指明了正确的方向,但事实证明 XSD 1.1 有专门为这种行为设计的东西:
OpenContents(参见: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
。
<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
仅允许序列末尾的元素。
还有为整个模式指定 defaultOpenContent
的选项:
<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>