Xml 是否可以在xs:choice中使用xs:group,以便只从组中选择一个?
我想在下面定义像AorBorC和ABCOrMore这样的复杂类型,它们应该只有预定义组(下面的group1)中的一个元素。在这里,我希望AOrBorC只能有一个出现在group1中的元素,而ABCOrMore可以有多个出现在group1中的元素。我可以在choice元素中单独填充选项,但这必须在多个位置完成,我想知道是否可以使用组来代替 我尝试了以下方法。但它允许组作为一个整体或其元素出现一次或多次 任何帮助都将不胜感激Xml 是否可以在xs:choice中使用xs:group,以便只从组中选择一个?,xml,xsd,xsd-validation,Xml,Xsd,Xsd Validation,我想在下面定义像AorBorC和ABCOrMore这样的复杂类型,它们应该只有预定义组(下面的group1)中的一个元素。在这里,我希望AOrBorC只能有一个出现在group1中的元素,而ABCOrMore可以有多个出现在group1中的元素。我可以在choice元素中单独填充选项,但这必须在多个位置完成,我想知道是否可以使用组来代替 我尝试了以下方法。但它允许组作为一个整体或其元素出现一次或多次 任何帮助都将不胜感激 <xs:group name="group1" > &
<xs:group name="group1" >
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="b" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="c" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:group>
<xs:complexType name="AorBorC">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:group ref="group1" />
</xs:choice>
</xs:complexType>
<xs:complexType name="ABCOrMore">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:group ref="group1" />
</xs:choice>
</xs:complexType>
要支持的XML: c_1 a_1 a_2 a_3
你选择错了地方
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://Scratch.ABC" targetNamespace="http://Scratch.ABC" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="AorBorC" type="group1" />
<xs:element minOccurs="1" maxOccurs="unbounded" name="ABCOrMore" type="group1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="group1">
<xs:choice>
<xs:element minOccurs="0" maxOccurs="1" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:schema>
XML
a_1
a_2
b_1
或者如果你愿意的话
<ns0:Root xmlns:ns0="http://Scratch.ABC">
<AorBorC>
<a>a_2</a>
</AorBorC>
<ABCOrMore>
<a>a_2</a>
<c>c_1</c>
<b>b_1</b>
</ABCOrMore>
</ns0:Root>
a_2
a_2
c_1
b_1
然后
编辑
澄清您的问题后,此模式应该符合要求
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://Scratch.ABC" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://Scratch.ABC" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="AorBorC" type="AorBorCType" />
<xs:element name="AorBorCMore" type="AorBorCMoreType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="AorBorCType">
<xs:choice>
<xs:element minOccurs="0" maxOccurs="1" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
<xs:complexType name="AorBorCMoreType">
<xs:choice>
<xs:element minOccurs="0" maxOccurs="unbounded" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:schema>
感谢您的快速帮助!我想了解是否有任何方法可以实现您发布的第二个XML(但只允许使用一种类型的ABCOrMore-在我的原始帖子中添加XML),而不在两个不同的xs:choice中重复a、b、c元素?您发布的解决方案可以工作,但它不维护a、b、c元素的一个列表。是否可以做同样的事情而不在两个列表中重复a、b、c元素?这正是列表2(AorBorCMoreType)所做的。maxOccurs低于该选项,因此您应该只能独占a、b或c。我测试了它,但一个以上的文件验证失败。当然,但我不想在单独的列表中定义AOrBOrCType和AORBorMoreType!我希望它们引用相同的列表。这是不可能的,因为它们的最大出现次数不同。
<ns0:Root xmlns:ns0="http://Scratch.ABC">
<AorBorC>
<a>a_2</a>
</AorBorC>
<ABCOrMore>
<a>a_2</a>
<c>c_1</c>
<b>b_1</b>
</ABCOrMore>
</ns0:Root>
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://Scratch.ABC" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://Scratch.ABC" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="AorBorC" type="AorBorCType" />
<xs:element minOccurs="1" maxOccurs="1" name="ABCOrMore" type="AorBorCMoreType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="AorBorCType">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element minOccurs="0" maxOccurs="1" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
<xs:complexType name="AorBorCMoreType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element minOccurs="0" maxOccurs="1" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://Scratch.ABC" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://Scratch.ABC" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="AorBorC" type="AorBorCType" />
<xs:element name="AorBorCMore" type="AorBorCMoreType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="AorBorCType">
<xs:choice>
<xs:element minOccurs="0" maxOccurs="1" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
<xs:complexType name="AorBorCMoreType">
<xs:choice>
<xs:element minOccurs="0" maxOccurs="unbounded" name="a" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="b" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="c" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:schema>