XML, the Extensible Markup Language
The first ideas for a uniform data language date back to the 1990s. The goal was a markup language for structured data that both people and machines could read. In 1998, the first specification of XML (eXtensible Markup Language) was finally published as version 1.0. Since then, XML has been used for the platform-independent exchange of data between different computer and database systems. A particular advantage of this markup language is that it is a metalanguage. On the basis of XML, application-specific languages can be created by defining structure and content.
The most prominent example of such a language is HTML (Hypertext Markup Language), the markup language that is known worldwide and used every day. It makes it possible to present web pages in a semantically structured way. Another major advantage of XML is its simple, text-based structure. As a character encoding, XML generally requires at least UTF-8. An XML document can therefore be created entirely with a simple text editor. XML offers further advantages, which we briefly present below.
Structures and Data Types in XML
XML itself has a simple and clearly defined structure. A valid document consists of elements, attributes, the corresponding value assignments, and element content. An XML structure may contain elements with and without attributes. There are also elements that may only have certain content, as well as elements that may in turn have many child elements. Empty elements, which have no content at all, are a special case. Using a list of exchange rates as an example, a basic XML structure can be represented as a simple tree structure as follows.
<wechselkurse>
<titel>Wechselkurse</titel>
<eintrag>
<fromcurrencycode>BTC</fromcurrencycode>
<tocurrencycode>CNY</ tocurrencycode >
<excangerate>43774.50023880</excangerate >
<lastrefreshed>2018-11-19 10:35:07</lastrefreshed>
<timezone>UTC</timezone>
</eintrag>
</wechselkurse>In addition to this simple, well-formed tree structure with plain elements and values such as <timezone>UTC</timezone>, XML allows a further form of structure. Every XML element can have its own attributes. The <lastrefreshed> element defined in the example above could therefore also be defined as follows.
<lastrefreshed date=“2018-11-19“ time=“10:35:07“/>This example also shows a valid shorthand notation that can be applied to elements. Normally, an element must consist of an opening tag, for example <lastrefreshed>, followed by an element value and a closing tag, for example </lastrefreshed>. If you use the combination of element and attributes, however, the element can also be closed directly with the end tag />, as shown above.
As mentioned, XML documents and their content can consist of any text characters within the UTF-8 encoding. Binary data is an exception. To include it correctly in an XML document, it must first be re-encoded. The proven Base64 encoding can be used for this, for example.
Deep Dive into XML
Beyond these standard tasks, XML offers many more options as a markup language. If you want to describe and use more complex data types with XML, you should, and in practice must, rely on a schema language. XSD (XML Schema Definition) provides a comprehensive schema language for this purpose. It can be used to define extended data types, separate schema instances of documents, and entire groups of such instances. A schema file of this kind can be recognized by its separate file extension .xsd.
XSD defines both simple and complex data types. The simple, classic data types include String, Decimal, Integer, Float, Boolean, Date, and Time. In XSD, a simple data type is defined with an identifier such as xs:string. These simple data types provide broad compatibility with programming languages such as C, Java, and SQL. There are also atomic data types such as URI, IDREF, and Language. None of the simple data types, however, may contain attributes or child elements in the XML structure.
In addition to the simple data types, XML can also represent its own complex data types. With an XML data type definition, complex element structures can be defined as a coherent unit. The exchange rate example shows how a new data type named “CurrencyExchange” is created in XSD.
<xs:complexType name=“CurrencyExchange“>
<xs:sequence>
<xs:element name=“fromcurrencycode“ type=“xs:string“/>
<xs:element name=“tocurrencycode“ type=“xs:string“/>
<xs:element name=“exchangerat“ type=“xs:float“/>
<xs:element name=“lastrefresheddate“ type=“xs:date“/>
<xs:element name=“lastrefreshedtime“ type=“xs:time“/>
<xs:element name=“lastrefreshedtimezone“ type=“xs:string“/>
</xs:sequence>
</xs:complexType>The complex data type defines its structure and its data types through its child elements. These can be combined in different ways with xs:sequence, xs:choice, or xs:all. xs:sequence defines a simple list of child elements that can occur once, several times, or not at all. xs:choice, by contrast, defines a selection list of child elements from which only one can be chosen. xs:all defines child elements that may each occur only once.
The XML schema definition offers many more options and advanced concepts. There are, for example, fundamental similarities with JSON (JavaScript Object Notation), a notation designed for JavaScript that we have presented in a separate article. Covering all of this would go well beyond the scope of this article. We therefore refer you to the list of links below, which points to resources on the web that describe these aspects in depth and illustrate them with examples.
Conclusion
As the framework of an extensible markup language, XML provides the basis for many established language definitions. Today the XML basis is used in numerous programming languages such as Java, JavaScript, and C. XML is also found in HTML, the native language of web page markup, and in the less prominent DOM (Document Object Model). A language definition named XHTML has accordingly existed for a long time. These are accompanied by widely used formatting languages with a similar structure, such as CSS and XSL. Here too, the original XML structure serves as the basis for formatting instructions, some of them complex, which can also cascade to the object and child elements in HTML, for example.
Further reading on XML and XSD