How Is XML Relevant?

Although the idea of using "metadata" to describe data only recently came to the fore, the concept has been around for a long time. Databases, for instance, are made up of tables, columns, views, and such, which are nothing more than metadata about the actual data contained in other tables. These items help describe the data without knowing what is contained in it. However, simply using names, perhaps combined with data types, does not solve all the problems present when trying to describe and ultimately understand data.

XML becomes the glue that binds what a piece of data actually is to what it is supposed to accomplish. It's used to describe all aspects of the data, ranging from near-physical properties to usage instructions, and its relationship to other data. This information can be used for human and machine-readable purposes, one of the true advantages of XML.

What Problem Does XML Solve?

Before the release and widespread adoption of XML, there were relatively few methods that could solve the problem of describing data in a standardized method so that anyone, including machines, could understand what the data meant. Work, on SGML and other electronic data such as the Electronic Data Interchange (EDI) for example, was done in the publishing world that demonstrated the potential of standardized systems, descriptions, and methods. However, SGML and EDI were expensive, either because of the software needed to handle the data or the training necessary to teach these complex markup languages.

Research helped create XML (a subset of SGML), which is able to describe data in a standardized way but is easier to understand and implement. Another reason this is useful is because today most everyone uses computers, so even the "nontechnical" people might need to be able to describe the data they have so others can understand it. A simple example is meeting.xml, shown in Listing 1-4.

Listing 1-4 meeting.xml: A quick XML example that can be used for nontechnical purposes.

 <?xml version="1.0"?> <meeting date="2001-07-29" time="14:30:00"> <actionItem who="fred">Send out minutes after meeting</actionItem> <actionItem who="sally">Set up presentation for Thursday</actionItem> <actionItem who="dan">Present product roadmap</actionItem> <actionItem who="tom">Send out agenda for next meeting</actionItem> </meeting> 

Even though we didn't describe what this code listing is, do you have any doubt as to what it represents? It represents action items from a meeting on July 29, 2001 at 14:30:00 (2:30 P.M.).

Another example is map.xml, shown in Listing 1-5, which defines directions from one location to another. These examples demonstrate how you can use XML to describe data that can be understood by a human, machine, or software.

Listing 1-5 map.xml: Using XML to describe directions to a particular location.

 <?xml version="1.0"?> <map> <start> <addr1>100 West Morgan St</addr1> <city>Raleigh</city> <state>NC</state> <zip>27603</zip> </start> <directions> <left distance="0.11 miles">W MORGAN ST</left> <left distance="0.11 miles">S WILMINGTON ST</left>  <left distance="0.44 miles">E EDENTON ST</left>  <right distance="0.09 miles">N WEST ST</right>  <left distance="0.02 miles">W JONES ST</left> </directions> <destination> <addr1>508 W Jones St</addr1> <city>Raleigh</city> <state>NC</state> <zip>27603</zip> </destination> </map> 

We specifically used these nontechnical examples to show that XML can solve many of the difficulties of exchanging or communicating data between two parties, human or machine. Although XML is often considered good only for moving data from A to B (in a manner where B understands what the data is), it can also be used for a variety of tasks, as the following examples demonstrate.

Why Is XML a Good Choice?

If you asked a few years ago whether XML was a good choice for defining your data, you would have received mixed responses. While most people would have agreed that XML would soon evolve as the de facto standard, they would have hedged their bets by saying that it might be wise to wait to ensure wide spread adoption. The flexibility and extensibility of using it as a means to describe data, on the other hand, positioned it for certain success. Today, when Microsoft Windows XP, Microsoft Office XP, and the .NET line of server products are on the shelves, XML is likely a requirement.

Let's take a simple example and demonstrate XML's flexibility by using it to define the dimensions of a door. We can use it not only to describe the data, but also to apply an Extensible Stylesheet Language Transformation (XSLT) to transform it into XHTML.

If we load the example in Listing 1-6 in an XML-supporting version of Internet Explorer, we should see something like Figure 1-4. This is a display of the text, with a default style sheet applied, after it is verified as well-formed. If something is mistyped, like the name of an element, an error will be generated.

Listing 1-6 door.xml: A document describing the dimensions of a wooden door.

 <?xml version="1.0"?> <door> <height type="inches">80</height> <width type="inches">36</width> <material type="wood"/> </door> 

Figure 1-4 Loading a simple XML document into Internet Explorer 5.

We could send this information to a supplier to see if they had any wooden doors that measure 36 by 80 inches, but that is not the limit of what we can do with this data. Suppose we wanted to display this information on a Web site, which would require us to format the information in HTML/XHTML. We can do this by including the following line in the document:

 <?xml-stylesheet type="text/xsl" href="transform.xsl"?> 

Adding this line, by itself, does not perform the necessary transformation from XML into XHTML; it only tells the parser that a corresponding style sheet for the document exists. We must now create a transform.xsl document to define the transformation, which would look like Listing 1-7.

Listing 1-7 transform.xsl: An XSLT style sheet responsible for transforming our XML document into XHTML.

 <?xml version="1.0"?> <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Door Information</title> </head> <body> <h1>Door Information</h1> <p> <strong> Material: </strong> <xsl:value-of select="//door/material/@type"/> <br/> <strong> Height: </strong> <xsl:value-of select="//door/height"/> (measured in <xsl:value-of select="//door/height/@type"/>) <br/> <strong> Width: </strong> <xsl:value-of select="//door/width"/> (measured in <xsl:value-of select="//door/width/@type"/>) </p> </body> </html> </xsl:template> </xsl:stylesheet> 

The Microsoft XML 3 parser (MSXML) introduced XSLT support into Microsoft's XML-supporting line of products. This parser, which was first available for Internet Explorer 4.01 Service Pack 1 as a separate download, had limited XSLT support. If you want to see this example work in Internet Explorer 5 or greater, you will need MSXML 4, which you can download from http://msdn.microsoft.com.

If we load door.xml in Internet Explorer the browser will apply the style sheet, transforming it from XML into XHTML, and you will see something like Figure 1-5.

This example illustrates why XML is a good choice for describing data. Data can be exchanged with partners, affiliates, and internal applications, and it can be used to describe your own internal content. With a little imagination you can see just how useful XML can be.

Figure 1-5 Transforming our door.xml document into XHTML.



XML Programming
XML Programming Bible
ISBN: 0764538292
EAN: 2147483647
Year: 2002
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net