A Quick Intro to XML Schemas

I mentioned in the previous post that some XML based file formats, like LibreOffice’s .ODT format, specify which tags you can use. These are specified in an XML Schema (older formats might use a Document Type Definition, as HTML does). You don’t really need to worry about how scheams are created; just be aware that they exist and what they do. Most schemas are stored as external files and then included in the XML document,

<greeting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.robertawood.com/example greeting.xsd">

Schemas can, though, be included inline rather than in an external file, but this is rarely done as then the schema becomes part of the document which needs to be described in the schema, which gets a bit confusing. Anyway just so you know what a schema actually looks like, here is the schema for the simple example used in the previous post.

<?xml version="1.0" encoding="utf-8" ?>
     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                attributeFormDefault="qualified"
                targetNamespace="http://www.robertawood.com/xmlExample">
         <xs:element name="greeting">
             <xs:complexType>
                 <xs:simpleContent>
                     <xs:extension base="xs:string">
                         <xs:attribute name="language" type="xs:string"/>
                     </xs:extension>
                 </xs:simpleContent>
             </xs:complexType>
         </xs:element>
     </xs:schema>
 

This schema first sets its own namespace before setting ‘attributeFormDefault=”qualified”’, meaning that any attributes a tag has are required to use a namespace, too (by default they aren’t). It then defines the namespace for document it is going to be describing (‘http://www.robertawood.com/xmlExample’) before going on to describe the document as consisting of a top level (root) tag called ‘greeting’ that can only contain an attribute called ‘language’ and a character string (i.e. the greeting itself). So, putting this all together, our example XML file would look something like this (note we can no longer say that it is standalone as we are using an external schema to describe it):

<?xml version="1.0" encoding="utf-8" ?>
 <!-- This is a simple example of an XML file -->
 <raw:greeting xmlns:raw="http://www.robertawood.com/xmlExample"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.robertawood.com/xmlExample greeting.xsd"
               raw:language="English">
     Hello, World!
 </raw:greeting>

Hopefully you could make sense of that. Don’t worry too much if you wouldn’t be able to write a simple file of your own. For our purposes all you really need to be able to do is read it.

Series Navigation<< A Quick Intro to XMLA Quick Intro to CSS >>

Leave a Reply

Your email address will not be published. Required fields are marked *