You already know that XML is a subset of SGML. And you know that XML is not intended to replace HTML but rather to complement it. In this section, we'll take a closer look at where the XML markup language fits between these two closely related languages.
SGML had been used for many years in sophisticated and highly complex publishing applications. Despite the fact that HTML, an application of SGML, is the standard for Web publishing, SGML itself never got any traction with the Web development community. This was primarily due to the complexity of SGML and the resulting overhead required to use it.
In August 1996, a group called the SGML Editorial Review Board (SGML ERB) formed within the World Wide Web Consortium (W3C). This group eventually became the XML Working Group. The group's goal was to create a generalized markup language that would be easy to implement on the Web. The SGML ERB worked with a larger group called the SGML Working Group, which later became the XML Special Interest Group. Together, these groups decided what parts of SGML would make up the XML language.
The focus of the XML Working Group was to include in XML only the parts of SGML that were needed for use in Web publishing. As a result, XML did away with much of the complexity and all of the optional features that have proved burdensome in SGML. Even with all that cutting, XML retains the primary benefits of SGML:
NOTE
You might wonder why it's beneficial to remove all of the optional features from SGML. Generally, the use of options increases the likelihood that a document will be incompatible with an application. That is, an application designed to process an SGML document with a particular set of options might not be able to process another SGML document with a different set of options. The designers of XML wanted to reduce, and preferably eliminate, incompatibility problems caused by options.
It is important to emphasize again that XML is a subset of SGML. Any valid XML document is also a valid SGML document. You might think of XML as "SGML Lite."
Many people think of XML as a replacement for HTML. Although this might be partly true, the two languages are more likely to be complementary to one another than in competition. In reality, the two languages operate at different levels with regard to how they treat data. For cases in which XML is used to structure and describe data on the Web, HTML will likely be used to format that data. Because most HTML pages store the data as well as the formatting for that data in the HTML code, any replacement of HTML will likely occur in the area of data storage. In many cases, XML might take over the job of data storage (in addition to retaining its job of data description), and HTML would be used just for formatting and scripting.
I promised that we would work with some XML code before the end of this chapter, so here we go. To help you get a picture of how XML and HTML can work together, let's take a look at how to use them to display data on a Web page. This sample will involve two documents: an XML document for the data and an HTML document for the formatting and display. Let's start by modifying the memo document created earlier in this chapter. As in all good programming books, our first code sample will display the words "Hello World!" The XML document, which you can find in the Chap02\Lst2_1.xml file on the companion CD, is shown in Code Listing 2-1.
Code Listing 2-1.
|
Next we'll create an HTML page to display the data in the memo. To do this, we'll need to add the XML processor to the page and use some script code to extract the data and put it in the format we want. The HTML code, which you don't need to understand in depth at this point, can be found in the Chap02\Lst2_2.htm file on the companion CD and is shown in Code Listing 2-2.
Code Listing 2-2.
|
You might be thinking that it takes a lot of code just to display that little memo. Although using XML is probably overkill in this case, the intent of this sample is to show how the content, or data, is kept separate from the formatting. So if you want to change the information in the memo, you only have to make changes to the XML code, and you don't have to wade through the HTML code to find what you need to change. I won't go through this code in depth at this point, since more in-depth information is covered in later chapters, but you can easily see how XML can be a powerful data storage mechanism, even without understanding any of the HTML code. To make changes to the memo, you follow these steps:
NOTE
If you copy Lst2_1.xml and Lst2_2.htm from the companion CD rather than running Setup, you need to turn off the Read-only attribute for the XML file so that you can make changes to it. To do this, right-click the Lst2_1.xml file in Windows Explorer and select Properties from the context menu. In the Lst2_1.xml Properties dialog box, clear the Read-only check box.
Your changes will be reflected in the display of the HTML document. Notice that the formatting is exactly the same as that of the original XML document, even though the data is different. You could even type the XML content in another language, such as German, and still it would display just as you intended, without your changing the HTML code. This is a simple example of how XML can be used to structure and store data. As you will see in later chapters, this is just the tip of the iceberg!
As you can see from the preceding sample, HTML and XML can work well together. Since all the content for the memo was kept separate from the HTML code, the content is easy to change and work with. And the HTML document can be used as a template to create many memo documents containing different data. In one sense, XML replaced portions of the HTML code because the HTML code no longer stored any of the data for the document. But the XML code did not constitute a total replacement of the HTML code.
NOTE
In later chapters, you'll see more of this collaboration between XML and HTML. You can even embed XML data fragments inside an HTML document as "XML Data Islands" As usual, the formatting is handled by HTML, but the data is kept to the strict structure and processing rules of XML.
Since it should now be obvious that the intent of XML is not to create a replacement for HTML, let's look at what the creators of XML did intend as they were developing the language.