Il seguente esempio è collegato all’articolo Linguaggio XML.

Viene qui presentato un documento XML contenente le informazioni inerenti ad una piccola libreria domestica. Sono inoltre allegati due distinti XML Schema collegati a tale documento. La seconda versione dello schema è stata ampliata con l’uso degli indici.

<?xml version="1.0" encoding="UTF-8"?>

<libreria_personale>

  <libro tipo="biografia">
    <titolo>Il mio primo libro</titolo>
    <autore nome="Mario" cognome="Rossi"/>
    <indice>
      <pagina>Così tutto è iniziato</pagina>
      <pagina>Così tutto è proseguito</pagina>
    </indice>
    <capitolo titolo="Così tutto è iniziato" num="1">
      <pagina>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</pagina>
      <pagina>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</pagina>
      <pagina>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</pagina>
    </capitolo>
    <capitolo titolo="Così tutto è proseguito" num="2">
      <pagina>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</pagina>
    </capitolo>
  </libro>
  
  <libro>
    <titolo>L'avventura continua</titolo>
    <autore nome="Luca" cognome="Verdi"/>
    <capitolo num="1" titolo="La svolta">
      <pagina>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</pagina>
      <pagina>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</pagina>
    </capitolo>
  </libro>
  
</libreria_personale>

Prima versione dell’XML Schema:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- definizione dei tipi semplici -->
  <xs:element name="titolo" type="xs:string"/>
  <xs:element name="pagina" type="xs:string"/>
  
  <!-- definizione degli attributi -->
  <xs:attribute name="titolo" type="xs:string"/>
  <xs:attribute name="num" type="xs:integer"/>
  <xs:attribute name="nome" type="xs:string"/>
  <xs:attribute name="cognome" type="xs:string"/>
  <xs:attribute name="tipo">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="biografia"/>
        <xs:enumeration value="giallo"/>
        <xs:enumeration value="avventura"/>
        <xs:enumeration value="romanzo"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  
  <!-- definizione dei tipi complessi -->
  <xs:element name="autore">
    <xs:complexType>
      <xs:attribute ref="nome" use="required"/>
      <xs:attribute ref="cognome" use="required"/>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="indice">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="pagina" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="capitolo">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="pagina" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute ref="titolo" use="required"/>
      <xs:attribute ref="num" use="required"/>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="libro">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="titolo"/>
        <xs:element ref="autore"/>
        <xs:element ref="indice" minOccurs="0"/>
        <xs:element ref="capitolo" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute ref="tipo"/>
    </xs:complexType>
  </xs:element>
  
  <xs:element name="libreria_personale">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="libro" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>

Seconda versione dell’XML Schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="tipolibroType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="biografia"/>
      <xs:enumeration value="giallo"/>
      <xs:enumeration value="avventura"/>
      <xs:enumeration value="romanzo"/>
    </xs:restriction>
  </xs:simpleType>
  
  <xs:complexType name="autoreType">
    <xs:attribute name="nome" type="xs:string" use="required"/>
    <xs:attribute name="cognome" type="xs:string" use="required"/>
  </xs:complexType>
  
  <xs:complexType name="indiceType">
    <xs:sequence>
      <xs:element name="pagina" type="xs:string" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  
  <xs:complexType name="capitoloType">
    <xs:sequence>
      <xs:element name="pagina" type="xs:string" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="titolo" type="xs:string" use="required"/>
    <xs:attribute name="num" type="xs:integer" use="required"/>
  </xs:complexType>
  
  <xs:complexType name="libroType">
    <xs:sequence>
      <xs:element name="titolo" type="xs:string"/>
      <xs:element name="autore" type="autoreType"/>
      <xs:element name="indice" type="indiceType" minOccurs="0">
        <xs:unique name="uniqueIndice">
          <xs:selector xpath="pagina"/>
          <xs:field xpath="."/>
        </xs:unique>
      </xs:element>
      <xs:element name="capitolo" type="capitoloType" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="tipo" type="tipolibroType"/>
  </xs:complexType>
  
  <xs:complexType name="libreriaPersonaleType">
    <xs:sequence>
      <xs:element name="libro" type="libroType" maxOccurs="unbounded">
        <xs:unique name="uniqueCapitolo">
          <xs:selector xpath="capitolo"/>
          <xs:field xpath="@titolo"/>
        </xs:unique>
        <xs:unique name="uniqueCapitoloNum">
          <xs:selector xpath="capitolo"/>
          <xs:field xpath="@num"/>
        </xs:unique>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  
  <xs:element name="libreria_personale" type="libreriaPersonaleType">
    <xs:unique name="uniquetitolo">
      <xs:selector xpath="libro/titolo"/>
      <xs:field xpath="."/>
    </xs:unique>
  </xs:element>
  
</xs:schema>