最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xml - Problem with adding XSD attribute to <all> type - Stack Overflow

programmeradmin1浏览0评论

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="; attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs=";>
  <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>

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./2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3./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>
Share Improve this question asked Jan 31 at 16:15 the_driverthe_driver 311 silver badge6 bronze badges 1
  • While asking an XSD question you need to provide a minimal reproducible example: (1) Well-formed input XML. (2) Your logic, and complete executable XSD(s) that tries to implement it. (3) XSD processor and its conformance with the XSD standards: 1.0, or 1.1. – Yitzhak Khabinsky Commented Jan 31 at 16:39
Add a comment  | 

1 Answer 1

Reset to default 0

Order of elements matters, see https://www.w3./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>
发布评论

评论列表(0)

  1. 暂无评论