XSD全部内选
XSD all inside choice
我有与this other question相反的需求。
我希望以下任何一项有效:
<a>
<b></b>
<c></c>
</a>
<a>
<d></d>
<e></e>
</a>
我尝试用两个 all
children 做一个 choice
,但那是无效的。
看起来我可以用两个 sequence
children 做一个 choice
,但我不希望实现者不得不担心元素的顺序。
看起来我也可以使用 group
s,但它们需要 ref
名称,并且内容的选项没有明显的分组名称。 (如果他们这样做了,我会以这种方式构建 XML。)
我一直在 XSD 1.1 documentation 工作。
我试图让我的 XSD 从数据结构中生成(主要是为了我可以回收注释等用于文档),所以简单很重要。
虽然这在 XSD 1.1 中在技术上是可行的,但解决方案不是很好...
我得到的解决方案将所有内容都放在 xs:all
中,然后使用 xs:assert
(仅限 XSD 1.1)来提供规则。
此解决方案虽然有效但难以阅读和维护,并且与 XSD 1.0 不兼容。我会倾向于您描述的 choice(seq(b,c), seq(d,e)) 解决方案,并将责任放在实施者身上以获得正确的顺序
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2019 BETA (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:all>
<xs:element name="b" minOccurs="0" />
<xs:element name="c" minOccurs="0" />
<xs:element name="d" minOccurs="0" />
<xs:element name="e" minOccurs="0" />
</xs:all>
<xs:assert test="((boolean(b) and boolean(c)) or (boolean(d) and boolean(e))) and not ((boolean(b) and boolean(c)) and (boolean(d) and boolean(e)))" />
</xs:complexType>
</xs:element>
</xs:schema>
我有与this other question相反的需求。
我希望以下任何一项有效:
<a>
<b></b>
<c></c>
</a>
<a>
<d></d>
<e></e>
</a>
我尝试用两个 all
children 做一个 choice
,但那是无效的。
看起来我可以用两个 sequence
children 做一个 choice
,但我不希望实现者不得不担心元素的顺序。
看起来我也可以使用 group
s,但它们需要 ref
名称,并且内容的选项没有明显的分组名称。 (如果他们这样做了,我会以这种方式构建 XML。)
我一直在 XSD 1.1 documentation 工作。
我试图让我的 XSD 从数据结构中生成(主要是为了我可以回收注释等用于文档),所以简单很重要。
虽然这在 XSD 1.1 中在技术上是可行的,但解决方案不是很好...
我得到的解决方案将所有内容都放在 xs:all
中,然后使用 xs:assert
(仅限 XSD 1.1)来提供规则。
此解决方案虽然有效但难以阅读和维护,并且与 XSD 1.0 不兼容。我会倾向于您描述的 choice(seq(b,c), seq(d,e)) 解决方案,并将责任放在实施者身上以获得正确的顺序
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2019 BETA (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:all>
<xs:element name="b" minOccurs="0" />
<xs:element name="c" minOccurs="0" />
<xs:element name="d" minOccurs="0" />
<xs:element name="e" minOccurs="0" />
</xs:all>
<xs:assert test="((boolean(b) and boolean(c)) or (boolean(d) and boolean(e))) and not ((boolean(b) and boolean(c)) and (boolean(d) and boolean(e)))" />
</xs:complexType>
</xs:element>
</xs:schema>