0
votes

I have following xml structure

<library>
  <propertySet>
    <SUPorganisationId></SUPorganisationId>
    <SUPdataCategory></SUPdataCategory>
    <SUPguId></SUPguId>
    <LIBuserNotice></LIBuserNotice>
  </propertySet>
</library>

Properties inside the propertySet can be appear once (minOccurs="0" maxOccurs="1") and could be any order. When I creating XSD I want to group some of the properties (prefix with SUP) for further using. So I have comeup with following xsd segments.

<xs:element name="propertySet">
  <xs:complexType>
    <xs:all>
      <xs:group ref="CORproperties"/>
      <xs:element name="LIBuserNotice" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:all>
  </xs:complexType>
<xs:element name="propertySet">

<xs:group name="CORproperties">
  <xs:all>
    <xs:element name="SUPorganisationId" type="xs:integer" minOccurs="0" maxOccurs="1"/>
    <xs:element name="SUPdataCategory" type="xs:integer" minOccurs="0" maxOccurs="1"/>
    <xs:element name="SUPguId" type="xs:string" minOccurs="0" maxOccurs="1"/>
  </xs:all>
</xs:group>

With this xsd I am getting errors saying that usage of xs:all is incorrect. I have forced to use xs:all because of there is no order of appearing properties. But it works fine if I use xs:sequence. Can anybody please direct me to the right path?

2

2 Answers

1
votes

You may use an <xs:extension> to do that. If you refactor your schema that way, it will work properly:

Warning: it is only available in XSD 1.1. In XSD 1.0, it is not allowed.

<xs:element name="propertySet">
    <xs:complexType>
     <xs:complexContent>
         <xs:extension base="CORProperties">
             <xs:all>
                 <xs:element name="LIBuserNotice" type="xs:string" minOccurs="0" maxOccurs="1"/>
             </xs:all>
         </xs:extension>
     </xs:complexContent>
    </xs:complexType>
</xs:element>

<xs:complexType name="CORProperties">
    <xs:all>
        <xs:element name="SUPorganisationId" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPdataCategory" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPguId" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:all>
</xs:complexType>
0
votes

From the annotation of xs:all, we see it can't have group:

<all
   id = ID
   maxOccurs = 1 : 1
   minOccurs = (0 | 1) : 1
   {any attributes with non-schema namespace . . .}>
   Content: (annotation?, element*)
</all>

Workaround one: change group to complexType

Of course, this will change the structure of your xml, but this way is more readable than option two.

Workaround two: accept duplicates

<xs:element name="propertySet">
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <xs:group ref="CORproperties"/>
            <xs:element name="LIBuserNotice" type="xs:string" minOccurs="0" maxOccurs="1"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

<xs:group name="CORproperties">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="SUPorganisationId" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPdataCategory" type="xs:integer" minOccurs="0" maxOccurs="1"/>
        <xs:element name="SUPguId" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:choice>
</xs:group>

More: