XML Schema Definition Language

Aus Mikiwiki
Version vom 1. März 2010, 17:00 Uhr von Michi (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Die XML-Schema Definition Language / XSD ist ein Standard des W3C.

Verwendung

Definition eines Elements "kontakt" vom Typ "string" in der XSD-Datei "kontakt.xsd".

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

Eine Beispieldatei "kontakt.xml" dazu:

<?xml version="1.0" encoding="UTF-8"?>
<kontakt xsi:noNamespaceSchemaLocation="kontakt.xsd" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

Eine weitere Beispieldatei "kontakt.xsd":

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="kontakt">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Vorname" type="xs:string" minOccurs="0" maxOccurs="3"/>
        <xs:element name="Nachname" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </element>
</xs:schema>

Die passende Datei "kontakt.xml" dazu.

<?xml version="1.0" encoding="UTF-8"?>
<kontakt xsi:noNamespaceSchemaLocation="kontakt.xsd" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<vorname>Matthias</vorname>
<nachname>Born</nachname>
</kontakt>


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="kontakt">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="vorname" type="xs:string" minOccurs="0" maxOccurs="3"/>
        <xs:element name="nachname" type="xs:string"/>
        <xs:element name="adresse">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="strasse" type="xs:string"/>
              <xs:element name="plz">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="stadt" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>


<?xml version="1.0" encoding="UTF-8"?>
<kontakt xsi:noNamespaceSchemaLocation="kontakt.xsd" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <vorname>Matthias</vorname>
    <nachname>Born</nachname>
    <adresse>
      <strasse>Finkenweg</strasse>
      <plz>45000</plz>
      <stadt>Berlin</stadt>
    </adresse>
</kontakt>

Erweiterung durch Element "adressbuch".

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="adressbuch">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="kontakt" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <!--  Kommentar -->
  <xs:element name="kontakt">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="vorname" type="xs:string" minOccurs="0" maxOccurs="3"/>
        <xs:element name="nachname" type="xs:string"/>
        <xs:element name="adresse">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="strasse" type="xs:string"/>
              <xs:element name="plz">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="stadt" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Zusätzliches Attribut mit lokal definiertem "simpleType".

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="adressbuch">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="kontakt" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <!--  Kommentar -->
  <xs:element name="kontakt">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="vorname" type="xs:string" minOccurs="0" maxOccurs="3"/>
        <xs:element name="nachname" type="xs:string"/>
        <xs:element name="adresse" maxOccurs="3">
          <xs:complexType>
            <xs:sequence>
              <xs:attribute name="art" use="required">
                <xs:simpleType>
                  <xs:restriction base="xsd:string">
                    <xs:enumeration value="geschäftlich"/>
                    <xs:enumeration value="privat"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:attribute>
              <xs:element name="strasse" type="xs:string"/>
              <xs:element name="plz">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="stadt" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>