Pages in this article
-
What is XML
-
Characteristics of XML
-
Structure of XML
-
XML Schemas
-
XML in Excel
-
XML Validation
-
Conclusion
XML Schemas
An XML file can have a schema definition (XSD) attached to it. A
schema definition determines the structure of the XML file. Other things
an XSD file may define are:
- The format of the data inside an element (string, number,
etcetera)
- Whether or not an element is mandatory
- If an element can be repeated.
To attach an XML file to a schema a reference to the schema file must
be included inside the XML file. The schema reference can be added as an
attribute of the root element, like this:
<companies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Schema.xsd">
The text in Bold builds the reference to the schema file schema.xsd,
which in this example must reside in the same folder as the xml file.
Without getting into the details of the exact syntax of a schema
definition here is an example tied to the file test1_en.xml:
Listing 1: Schema.xsd
<?xml version='1.0' encoding='UTF-16'?>
<!-- XmlMap.DataBinding.SourceUrl: C:\Data\OfficeMagazine\XML\test2.xml -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="companies">
<xsd:complexType>
<xsd:sequence minOccurs="0">
<xsd:element minOccurs="0" name="company" >
<xsd:complexType>
<xsd:sequence minOccurs="0">
<xsd:element minOccurs="0" type="xsd:string" name="companyname" />
<xsd:element minOccurs="0" maxOccurs="unbounded" name="employee" >
<xsd:complexType>
<xsd:sequence minOccurs="0">
<xsd:element minOccurs="1" type="xsd:string" name="code" />
<xsd:element minOccurs="1" type="xsd:string" name="name" />
<xsd:element minOccurs="0" type="xsd:string" name="street" />
<xsd:element minOccurs="0" type="xsd:string" name="Houseno" />
<xsd:element minOccurs="0" type="xsd:string" name="areacode" />
<xsd:element minOccurs="0" type="xsd:string" name="place" />
<xsd:element minOccurs="0" type="xsd:string" name="phone" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Such a schema is obviously less easy to read than the accompanying
XML file. More information
on schemas.
Extensible Stylesheets
With Extensible Stylesheets (xsl files) xml files can be converted to
other formats, e.g. to SpreadsheetML, the XML format that supports
spreadsheets. How these stylesheets work is outside the scope of this
article. More information about
extensible stylesheets by the w3c organisation.