2013-10-11 27 views
2

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

我尝试了以下。但是它允许一个或多个团体作为一个整体而不是它的元素。

任何帮助,将不胜感激!

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

回答

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

感谢您的快速帮助!我想了解是否有任何方法可以实现您发布的第二个XML(但只允许使用一种类型的ABCOrMore--在原始文章中添加XML),而不必在两个不同的xs中选择a,b,c元素:choice ? – prixa

+0

你去哪里了,在最后更新了一个新的模式 – Dijkgraaf

+0

你发布的解决方案的作品,但它并没有维护一个列表中的a,b,c元素。是否有可能做同样的事情,而不是重复2列表中的a,b,c元素? – prixa

相关问题