Example Schema

www.altova.com このトピックを印刷 前のページ 1つ上のレベル 次のページ

ホーム >  Code Generator > Generating Code from XML Schemas or DTDs > Example: Using the Schema Wrapper Libraries >

Example Schema

The schema used in this example describes a library of books. The complete definition of the schema is shown below. Save this code listing as Library.xsd if you want to get the same results as this example. You will need this schema to generate the code libraries used in this example.


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.nanonull.com/LibrarySample" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.nanonull.com/LibrarySample" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="Library">
          <xs:element name="Book" type="BookType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:attribute name="LastUpdated" type="xs:dateTime"/>
  <xs:complexType name="BookType">
        <xs:element name="Title" type="xs:string"/>
        <xs:element name="Author" type="xs:string" maxOccurs="unbounded"/>
    <xs:attribute name="ID" type="xs:integer" use="required"/>
    <xs:attribute name="Format" type="BookFormatType" use="required"/>
  <xs:complexType name="DictionaryType">
        <xs:extension base="BookType">
              <xs:element name="FromLang" type="xs:string"/>
              <xs:element name="ToLang" type="xs:string"/>
  <xs:simpleType name="BookFormatType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Hardcover"/>
        <xs:enumeration value="Paperback"/>
        <xs:enumeration value="Audiobook"/>
        <xs:enumeration value="E-book"/>


Library is a root element of a complexType which can be graphically represented as follows in the schema view of XMLSpy:


As shown above, the library has a LastUpdated attribute (defined as xs:dateTime), and stores a sequence of books. Each book is an xs:complexType and has two attributes: an ID (defined as xs:integer), and a Format. The format of any book can be hardcover, paperback, audiobook, or e-book. In the schema, Format is defined as xs:simpleType which uses an enumeration of the above-mentioned values.


Each book also has a Title element (defined as xs:string), as well as one or several Author elements (defined as xs:string).


The library may also contain books that are dictionaries. Dictionaries have the type DictionaryType, which is derived by extension from the BookType. In other words, a dictionary inherits all attributes and elements of a Book, plus two additional elements: FromLang and ToLang, as illustrated below.


The FromLang and ToLang elements store the source and destination language of the dictionary.


An XML instance file valid according to the schema above could therefore look as shown in the listing below (provided that it is in the same directory as the schema file):


<?xml version="1.0" encoding="utf-8"?>
<Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.nanonull.com/LibrarySample" xsi:schemaLocation="http://www.nanonull.com/LibrarySample Library.xsd" LastUpdated="2016-02-03T17:10:08.4977404">  
  <Book ID="1" Format="E-book">
    <Title>The XMLSpy Handbook</Title>
  <Book ID="2" Format="Paperback" xmlns:n1="http://www.nanonull.com/LibrarySample" xsi:type="n1:DictionaryType">
    <Title>English-German Dictionary</Title>
    <Author>John Doe</Author>


The next topics illustrate how to read from such a file programmatically, or write to such a file programmatically. To begin, generate the schema wrapper code from the schema above, using the steps described in Generating Code from XML Schemas or DTD.

(C) 2019 Altova GmbH