2

我想在下面定义像 AorBorC 和 ABCOrMore 这样的复杂类型,它们应该只有一个来自预定义组(下面的 group1)的元素。在这里,我希望 AOrBorC 只能从 group1 中出现 1 个元素,而 ABCOrMore 可以从 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 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>

4

1 回答 1

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>
于 2013-10-11T03:46:42.703 回答