XML (Extensible Markup Language) is designed to separate content from presentation by focusing on data representation rather than how the data is displayed. This separation is achieved through the following key principles:
-
Content Representation in XML
-
Presentation Is Handled Separately
- The presentation of XML data is defined externally, using technologies like:
- XSLT (Extensible Stylesheet Language Transformations): To transform XML data into HTML or other formats for display.
- CSS (Cascading Style Sheets): To style the XML data for presentation.
- This allows the same XML content to be presented in different ways without modifying the content itself.
- Example: XML + XSLT
Here is an example where XML data is styled using XSLT:
XML Content:
<book>
<title>Learning XML</title>
<author>John Doe</author>
<year>2024</year>
</book>
XSLT for Presentation:
<xsl:template match="book">
<h1><xsl:value-of select="title"/</h1>
<p>Author: <xsl:value-of select="author"/></p>
<p>Year: <xsl:value-of select="year"/></p>
</xsl:template>
- The XSLT extracts data from the XML tags and determines how it will look (e.g.,
<h1>
, <p>
tags for presentation).
-
Advantages of Content-Presentation Separation
-
Reusability:
- XML content can be reused for different presentations, such as:
- A web page (HTML).
- A PDF or other printable formats.
- A database import/export file.
-
Consistency:
- Content remains consistent regardless of how it is displayed.
- Presentation changes do not require modifications to the content.
-
Maintenance:
- Content and presentation can be updated independently. If the layout changes, the XML data remains untouched.
-
Portability:
- XML can be used across platforms and applications because it is platform-agnostic.
Summary
In XML, data (content) is stored in a structured, logical way without any formatting or style. The presentation is delegated to external tools like XSLT, CSS, or applications that consume the XML. This clear separation enhances flexibility, maintainability, and reusability of the data.
Imagine that while driving through unknown territory, you could ask an onboard car computer for directions to the nearest gas station.
For that to be possible, the markup language used for this application must be specific not just in terms of document structure but also about the actual content contained within the documents. XML (eXtensible Markup Language) is a meta-markup language that expressly separates content from presentation. Using user-defined XML elements, or tags, XML documents provide meaning to the data contained in these documents.
Presentation of XML documents may take several different formats, including rendering an XML document in a browser.
This module defines XML, discusses its origins and applications, and describes the evolution of markup and metalanguages.
By the end of the module, you will have the skills and knowledge necessary to:
- Describe markup languages
- Describe metalanguages
- Describe the limitations of HTML
- Define XML
- List the goals of XML
- Describe approaches to using XML
Throughout this course the terms "elements" and "tags" are used interchangeably.
XML will be referred to as a language and as a metalanguage. The next lesson describes markup languages in general.
There are two main uses for XML:
- One is a way to represent low-level data, for example configuration files.
- The second is a way to add metadata to documents; for example, you may want to stress a particular sentence in a report by putting it in italics or bold.
The first usage for XML is meant as a replacement for the more traditional ways this has been done before, usually by means of lists of name/value pairs as seen in Java's Property files.
The second application of XML is similar to how HTML files work.
The document text is contained in an overall container, the <body> element, with individual phrases surrounded by <i> or <b> tags.
For both of these scenarios there has been a variety of techniques devised over the years. The problem with these disparate approaches has been more apparent than ever, since the increased use of the Internet and extensive existence of distributed applications, particularly those that rely on components designed and managed by different parties. That problem is one of intercommunication.
It's certainly possible to design a
distributed system that has two components, one outputting data using a Windows INI file and the other which turns it into a Java Properties format.
Unfortunately, it means a lot of development on both sides, which should not really be necessary and takes away resources from the main objective, which is developing new functionality that delivers business value.
XML was conceived as a solution to this kind of problem; it is meant to make passing data between different components much easier and relieve
the need to continually worry about different formats of input and output, freeing up developers to concentrate on the more important aspects of coding such as the
business logic. XML is also seen as a solution to the question of whether files should be easily readable by software or by humans; XML attempts to fulfill both objectives.