是否可以在xs:choice中使用xs:group,以便只选择组中的一个?

时间:2013-10-11 01:23:56

标签: xml xsd xsd-validation

我想在下面定义复杂类型,如AorBorC和ABCOrMore,它们应该只有一个来自预定义组的元素(下面的group1)。在这里,我希望AOrBorC只能在group1中出现1次元素,而ABCOrMore可以从group1中选择多次出现的元素。我可以在choice元素中单独填充选项,但这必须在多个地方完成,我想知道是否可以使用组。

我尝试了以下内容。但它允许整个组中出现一次或多次,或者它的元素。

任何帮助将不胜感激!

<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 to be supported:
<ns0:Root xmlns:ns0="http://Scratch.ABC">
  <AorBorC>
    <c>c_1</c>
  </AorBorC>
  <ABCOrMore>
    <a>a_1</a>
    <a>a_2</a>
    <a>a_3</a>
  </ABCOrMore>

1 个答案:

答案 0 :(得分:0)

您可以选择错误的地方

<?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

<ns0:Root xmlns:ns0="http://Scratch.ABC">
  <AorBorC>
    <a>a_1</a>
  </AorBorC>
  <ABCOrMore>
    <a>a_2</a>
  </ABCOrMore>
  <ABCOrMore>
    <b>b_1</b>
  </ABCOrMore>
</ns0:Root>

或者如果你想要

<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>