Подтвердить что ты не робот

Элемент XSD с атрибутами и дочерними элементами

Я хочу знать правильный синтаксис для определения элемента, содержащего как атрибуты, так и дочерние элементы (если возможно). Например:

<component type="A" binding="B">
  <operation name="X">
    <input type="C" />
  </operation>

  <event name="Y">
    <output type="D" />
  </event>
</component>

Как вы можете видеть, оба элемента, компонента и события имеют как атрибуты, так и дочерний элемент. Можно ли это определить в XSD? Как?

Большое спасибо!

4b9b3361

Ответ 1

Это один из возможных способов определения XSD, соответствующего вашему XML; при обучении XSD вы можете зачислить помощь инструмента, который предоставляет вам XSD, начиная с одного или нескольких файлов примеров XML.

<?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>

Как вы это сделаете, вы можете начать настройку min/maxOccurs, использовать (обязательно/необязательно), повторное использование определений и т.д. Сгенерированный XSD является хорошей отправной точкой, но обычно заканчивается тем, что редактируется так или иначе...

Ответ 2

Ниже приводится решение, которое работает:   

<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" />