XSD element with both attributes and child elements

后端 未结 2 1718
无人及你
无人及你 2020-12-29 18:37

I want to know the correct syntax for defining an element containing both attributes and child elements (if possible). For example:



        
相关标签:
2条回答
  • 2020-12-29 18:48

    I am giving below, a solution that works:

    <xs:simpleType name="inputTypeType">
            <xs:restriction base="xs:string" />
    </xs:simpleType>
    
    <xs:complexType name="inputType">
        <xs:attribute name="type" type="inputTypeType"/>            
    </xs:complexType>
    
    <xs:simpleType name="operationNameType">
            <xs:restriction base="xs:string" />
    </xs:simpleType>
    
    <xs:complexType name="operationType">
      <xs:sequence>
        <xs:element name="input"   type="inputType" />
      </xs:sequence>
        <xs:attribute name="name" type="operationNameType"/>
    </xs:complexType>
    
    
    
    <xs:simpleType name="outputTypeType">
            <xs:restriction base="xs:string" />
    </xs:simpleType>
    
    <xs:complexType name="outputType">
        <xs:attribute name="type" type="outputTypeType"/>           
    </xs:complexType>
    
    <xs:simpleType name="eventNameType">
            <xs:restriction base="xs:string" />
    </xs:simpleType>
    
    <xs:complexType name="eventType">
      <xs:sequence>
        <xs:element name="output"   type="outputType" />
      </xs:sequence>
        <xs:attribute name="name" type="eventNameType"/>
    </xs:complexType>   
    
    
    <xs:simpleType name="typeType">
            <xs:restriction base="xs:string" />
    </xs:simpleType>
    
    <xs:simpleType name="bindingType">
            <xs:restriction base="xs:string" />
    </xs:simpleType>
    
    
    
    
    <xs:complexType name="componentType">
        <xs:sequence>        
            <xs:element name="operation"   type="operationType" />
            <xs:element name="event" type="eventType" />
        </xs:sequence>
        <xs:attribute name="type" type="typeType"/>
        <xs:attribute name="binding" type="bindingType"/>       
    </xs:complexType>
    
    
    <xs:element name="component" type="componentType" />
    

    0 讨论(0)
  • 2020-12-29 18:52

    This is one possible way to define an XSD matching your XML; when learning XSD, you could enroll the help of a tool that infers the XSD for you, starting from one or more XML sample files.

    <?xml version="1.0" encoding="utf-8"?>
    <!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
    <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="component">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="operation">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:element name="input">
                    <xsd:complexType>
                      <xsd:attribute name="type" type="xsd:string" use="required" />
                    </xsd:complexType>
                  </xsd:element>
                </xsd:sequence>
                <xsd:attribute name="name" type="xsd:string" use="required" />
              </xsd:complexType>
            </xsd:element>
            <xsd:element name="event">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:element name="output">
                    <xsd:complexType>
                      <xsd:attribute name="type" type="xsd:string" use="required" />
                    </xsd:complexType>
                  </xsd:element>
                </xsd:sequence>
                <xsd:attribute name="name" type="xsd:string" use="required" />
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name="type" type="xsd:string" use="required" />
          <xsd:attribute name="binding" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    

    As you walkthrough this, you may start tweaking min/maxOccurs, use (required/optional), reusing definitions, etc. A generated XSD is a good starting point, but typically ends up being edited one way or another...

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