<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- Reusable type for elements that may be empty or contain text -->
  <xs:simpleType name="StringOrEmpty">
    <xs:restriction base="xs:string">
      <xs:minLength value="0"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- Root -->
  <xs:element name="Netlist">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Components" type="ComponentsType"/>
        <xs:element name="Nets" type="NetsType"/>
      </xs:sequence>
      <xs:attribute name="BySoftware" type="xs:string" use="required"/>
      <xs:attribute name="ByModule"   type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>

  <!-- Components -->
  <xs:complexType name="ComponentsType">
    <xs:sequence>
      <xs:element name="Component" type="ComponentType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ComponentType">
    <xs:sequence>
      <xs:element name="Footprint" type="StringOrEmpty"/>
      <xs:element name="Value"     type="StringOrEmpty"/>
      <xs:element name="Device"    type="StringOrEmpty"/>
      <xs:element name="Pins"      type="PinsType"/>
      <xs:element name="Attribute" type="AttributeType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="Refdes" type="xs:string" use="required"/>
  </xs:complexType>

  <!-- Pins -->
  <xs:complexType name="PinsType">
    <xs:sequence>
      <xs:element name="Pin" type="PinType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="PinType">
    <xs:sequence>
      <xs:element name="PinName" type="StringOrEmpty"/>
      <xs:element name="Attribute" type="AttributeType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="Number" type="xs:string" use="required"/>
  </xs:complexType>

  <!-- Attribute -->
  <xs:complexType name="AttributeType">
    <xs:attribute name="Key"   type="xs:string" use="required"/>
    <xs:attribute name="Value" type="xs:string" use="required"/>
  </xs:complexType>

  <!-- Nets -->
  <xs:complexType name="NetsType">
    <xs:sequence>
      <xs:element name="Net" type="NetType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="NetType">
    <xs:sequence>
      <xs:element name="Connection" type="ConnectionType" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="Attribute"  type="AttributeType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="required"/>
  </xs:complexType>

  <!-- Connection -->
  <xs:complexType name="ConnectionType">
    <xs:attribute name="Refdes" type="xs:string" use="required"/>
    <xs:attribute name="PinNum" type="xs:string" use="required"/>
  </xs:complexType>

</xs:schema>

