XSD allowing both simpleType and complexType content for same element?

后端 未结 2 1227
长情又很酷
长情又很酷 2020-12-21 13:57

I have a situation where I have different XMLs that will have different types of properties. Sometimes the element HEADER could have just a node or some XMLs co

相关标签:
2条回答
  • 2020-12-21 14:21

    If the instances already exist, and you can't change them, and you are trying to write an XSD schema to describe them, and it has to be one schema that describes them all, then your options are very limited. As far as I'm aware the only solution is to define HEADER with mixed content -- and that's a lousy solution. It can be improved a bit (though not much) by using XSD 1.1 assertions.

    If you can remove any of these requirements (e.g if you can change the instance documents, or if you can use RelaxNG to do the validation, or if you can use a different schema for each document type) then you have a chance of a more satisfactory solution.

    0 讨论(0)
  • 2020-12-21 14:32

    In XSD, you cannot allow both simple and complex content unless you're willing to have mix elements and text via mixed="true" (in this case Example 1 is not needed). You could then used XSD 1.1 assertions to exclude both from appearing simultaneously.

    <xs:element name="HEADER">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="HEAD">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="Label" type="xs:string" use="required" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="Label" type="xs:string" use="required" />
    </xs:complexType>
    </xs:element>
    

    However, you're swimming against the current here. Instead, accept that you really have two different entities with two different content models and name the different entities differently: SIMPLE_HEADER and COMPLEX_HEADER comes to mind. Then you can use xs:choice/maxOccurs="unbounded" on Details to allow simple and complex headers to be freely interspersed.

    0 讨论(0)
提交回复
热议问题