Skip to content

Instantly share code, notes, and snippets.

@stungeye
Last active October 26, 2016 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stungeye/5f217777a5ce0d15e5d9 to your computer and use it in GitHub Desktop.
Save stungeye/5f217777a5ce0d15e5d9 to your computer and use it in GitHub Desktop.
XML Schema Examples

note.xml (Complex root node with children of simple types.)

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

note.xsd (Complex root node with children of simple types.)

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

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

note.xml (One complex type with an attribute and simple children.)

<note date="2015-09-01">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

note.xsd (One complex type with an attribute and simple children.)

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

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="date" type="xs:date" use="required">
  </xs:complexType>
</xs:element>

</xs:schema>

note.xml (Date is now an element. Multiple tos. Body optional.)

<note>
  <date>2015-09-01</date>
  <to>Tove</to>
  <to>Wally</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

note.xsd (Date is now an element. Multiple tos. Body optional.)

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

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="date" type="xs:date"/>
      <xs:element name="to" type="xs:string" maxOccurs="unbounded"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

note.xml (Date is now a custom complex type.)

<note>
  <date>
  	<day>23</day>
  	<month>September</month>
  	<year>2016</year>
  </date>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

note.xsd (Date is now a custom complex type.)

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="note">
      <xs:complexType>
        <xs:sequence>
            <xs:element name="date" type="nestedDate" />
            <xs:element name="to" type="xs:string" maxOccurs="unbounded" />
            <xs:element name="from" type="xs:string" />
            <xs:element name="heading" type="xs:string" />
            <xs:element name="body" type="xs:string" minOccurs="0" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>   

    <xs:complexType name="nestedDate">
        <xs:sequence>
            <xs:element name="year" type="xs:integer" />
            <xs:element name="month" type="xs:string" />
            <xs:element name="day" type="xs:integer"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

note.xml - Final Schema

<?xml version="1.0" encoding="utf-8" ?>
<note>
  <date>
  	<year>2016</year>
  	<month>January</month>
  	<day>2</day>
  </date>
  <to>Tove</to>
  <to>Wally</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body language="english">Don't forget to feed the goats.</body>
</note>

####note.xsd - Final Schema

Includes:

  • A custom complex type for the nested date date.
  • Date month restricted using regular expression pattern to English month names or their abbreviations.
  • Date day restricted to integers from 1 to 31.
  • Allowance for multiple "to" elements.
  • Body element is optional.
  • A language attribute for the body element (by way of a custom type extending a simple type.)

Here it is:

<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="note" type="emailNote" />

  <xs:complexType name="emailNote" >
    <xs:sequence>
      <xs:element name="date" type="nestedDate"/>
      <xs:element name="to" type="xs:string" maxOccurs="unbounded"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="internationalizedString" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  
  <xs:complexType name="nestedDate">
    <xs:sequence>
      <xs:element name="year" type="xs:integer"/>
      <xs:element name="month" type="englishMonth"/>
      <xs:element name="day" type="dayOfMonth"/>
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="englishMonth">
    <xs:restriction base="xs:string">
      <xs:pattern value="Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sept(ember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="dayOfMonth">
  	<xs:restriction base="xs:positiveInteger">
      <xs:maxInclusive value="31" />
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="internationalizedString">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="language" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

</xs:schema>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment