xmlxsd

Problem with adding XSD attribute to <all> type


i have an XSD file. I try to add an attribute but i have a compilation problem, the XSD is not valid.

If i test with sequence and not all type, i success to add an atribute, this is not difficult, for example this code work... :

<xs:element maxOccurs="unbounded" name="trade">
  <xs:complexType>
    <xs:sequence>                                
      <xs:element name="amount" type="xs:string" />
      <xs:element name="lot" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="crypto" type="xs:string" use="required" />                                
  </xs:complexType>
</xs:element>

but with the all type who is mandatory in my work task, it seem diferent... i am a bit confused with that, i dont understand why it s different and how to process.

For this exemple i tryed to add the attribute "Crypto" in "trades" element. How can i resolve my problem ?

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="positionList">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="positions">
          <xs:complexType>
            <xs:all>
              <xs:element name="Details">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="AccountDetails">
                      <xs:complexType>
                        <xs:all>
                          <xs:element name="PK_Position"   minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                            <xs:element maxOccurs="unbounded" name="trade">
                              <xs:complexType>
                                <xs:attribute name="crypto" type="xs:string" use="required" />
                                <xs:all>
                                  <xs:element name="amount"   minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                  <xs:element name="lot"       minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                </xs:all>
                              </xs:complexType>
                            </xs:element>
                          <xs:element name="openingHours">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element maxOccurs="unbounded" name="day">
                                  <xs:complexType>
                                    <xs:all>
                                      <xs:element name="day"            minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                      <xs:element name="open1"          minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                      <xs:element name="close1"         minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                      <xs:element name="open2"          minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                      <xs:element name="close2"         minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                    </xs:all>
                                  </xs:complexType>
                                </xs:element>
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                          <xs:element name="idBrokerList">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element maxOccurs="unbounded" name="idBrokerList">
                                  <xs:complexType>
                                    <xs:all>
                                      <xs:element name="brokerName"             minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                    </xs:all>
                                  </xs:complexType>
                                </xs:element>
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                          <xs:element name="margin"       minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                          <xs:element name="capital"       minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                          <xs:element name="margin_free"             minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                        </xs:all>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:all>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Solution

  • Order of elements matters, see https://www.w3.org/TR/xmlschema11-1/#declare-type for the allowed and required order; try to move the xs:attribute after the xs:all e.g.

    <xs:complexType>
    
                                <xs:all>
                                  <xs:element name="amount"   minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                  <xs:element name="lot"       minOccurs="1"   maxOccurs="1"   nillable="true"   type="xs:string"/>
                                </xs:all>
                                <xs:attribute name="crypto" type="xs:string" use="required" />
    </xs:complexType>