gpt4 book ai didi

xml - 使用 xs :extension 忽略元素的顺序

转载 作者:数据小太阳 更新时间:2023-10-29 01:40:01 27 4
gpt4 key购买 nike

如何设计我的 xsd 以忽略元素的顺序?

<root> <a/> <b/> </root>

<root> <b/> <a/> </root>

我需要使用 extension 出于代码生成的原因,所以我使用 all 尝试了以下操作:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com/test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:t="http://www.example.com/test" >

<xs:complexType name="BaseType">
<xs:all>
<xs:element name="a" type="xs:string" />
</xs:all>
</xs:complexType>

<xs:complexType name="ExtendedType">
<xs:complexContent>
<xs:extension base="t:BaseType">
<xs:all> <!-- ERROR -->
<xs:element name="b" type="xs:string" />
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:element name="root" type="t:ExtendedType"></xs:element>
</xs:schema>

虽然此 xsd 无效,但在 <!-- ERROR --> 报告了以下错误:

cos-all-limited.1.2: An all model group must appear in a particle with {min occurs} = {max occurs} = 1, and that particle must be part of a pair which constitutes the {content type} of a complex type definition.

cos-all-limited.1.2 的文档说:

1.2 the {term} property of a particle with {max occurs}=1 which is part of a pair which constitutes the {content type} of a complex type definition.

我不太明白这个(既不是 xsd 也不是英语母语人士 :))。


我是在做错事,还是在做错事,还是没有办法做到这一点?

最佳答案

主要编辑 最初我错过了您需要使用 xsd:extension 的要求。请注意,xsd:extension 的工作方式就好像有 xsd:sequence,其中基本类型的内容后跟扩展类型的内容。正如 XML Schema Primer 所说:

When a complex type is derived by extension, its effective content model is the content model of the base type plus the content model specified in the type derivation. Furthermore, the two content models are treated as two children of a sequential group.

因此,似乎使这项工作可行的唯一方法是拥有一个空的基本类型并将整个替代方案存储在扩展类型中,反之亦然(基本类型中的所有内容和一个空的扩展)。像这样:

<xsd:complexType name="ExtendedType">
<xsd:complexContent>
<xsd:extension base="BaseType">
<xsd:choice>
<xsd:sequence>
<xsd:element name="a" type="xsd:string"/>
<xsd:element name="b" type="xsd:string"/>
</xsd:sequence>
<xsd:sequence>
<xsd:element name="b" type="xsd:string"/>
<xsd:element name="a" type="xsd:string"/>
</xsd:sequence>
</xsd:choice>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BaseType"/>

<xsd:element name="root" type="ExtendedType"/>

关于xml - 使用 xs :extension 忽略元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2689579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com