In the examples above, we worked with XML as the data source and HTML as the display mechanism. They were kept as separate documents that interacted when the HTML file was viewed. Another option is to use XML data islands. Data islands allow authors to include XML fragments directly in an HTML document. There are pros and cons to this approach. On the plus side:
There can also be some negatives, depending on how the data island is used:
XML data islands can be either inline XML or linked XML documents.
To use inline XML data islands, you simply insert XML code directly into an HTML page. The XML "document" is contained inside a set of <XML></XML> tags. To demonstrate this, we'll modify Code Listing 21-1 by embedding email.xml into it and changing the code somewhat, as shown in Code Listing 21-4. This will produce the exact same output as the earlier example, shown in Figure 21-1.
Code Listing 21-4.
<HTML> <HEAD> <TITLE>Code Listing 21-4</TITLE> <XML ID="email"> <EMAIL> <TO>Bill_Pardi@hotmail.com</TO> <FROM>Eric_Schurman@hotmail.com</FROM> <CC>DHTML_in_Action@hotmail.com</CC> <SUBJECT>Basic email</SUBJECT> <BODY>This is an XML-based email.</BODY> </EMAIL> </XML> <SCRIPT LANGUAGE="JavaScript"> function start(){ xmlDoc=email var root=xmlDoc.documentElement todata.innerText=root.childNodes.item(0).text fromdata.innerText=root.childNodes.item(1).text ccdata.innerText=root.childNodes.item(2).text subjectdata.innerText=root.childNodes.item(3).text bodydata.innerText=root.childNodes.item(4).text } </SCRIPT> </HEAD> <BODY onload="start()"> <B>To:</B><SPAN ID="todata"></SPAN><BR> <B>From:</B><SPAN ID="fromdata"></SPAN><BR> <B>CC:</B><SPAN ID="ccdata"></SPAN><BR> <B>Subject:</B><SPAN ID="subjectdata"></SPAN><BR> <B>Body:</B><SPAN ID="bodydata"></SPAN><BR> </BODY> </HTML> |
First note that while the inline XML data is enclosed in <XML> tags, the <XML> element is not the root node of the data. Instead, it is an HTML tag that identifies to the browser that whatever is contained inside it should be treated as XML. Notice also that the <XML> tag has an ID assigned to it so that it can be accessed by script. The new code xmlDoc=email is the equivalent of instantiating an XMLDOM ActiveXObject and then loading a particular XML document. In fact, the same processor is being used; this is just a different way of accessing it. In addition to the inline data island, there is another way to use the <XML> tag—by linking to an external XML document.
To link the <XML> tag to an XML document, you simply use the SRC attribute of the <XML> tag. We can replace the entire XML element in the previous document with the following line of code.
<XML ID="email" SRC="email.xml"></XML> |
This works exactly like the preceding example. The advantage here is that the XML data remains separate from the HTML document. In essence, this method works very much like using the ActiveX control method, except that the browser takes the responsibility for instantiating the control and loading the document.
The <XML> tag includes some additional attributes that provide more control over what happens when the document is loaded.
The VALIDATEONPARSE attribute indicates whether the document should be validated when it is parsed. Its default value is true. It is used as shown below:
<XML ID="xmlDoc" SRC="xmldoc.xml" VALIDATEONPARSE="false"></XML> |
The ASYNC attribute indicates whether the document should be downloaded asynchronously. Its default value is true. Usage is shown in the sample below:
<XML ID="xmlDoc" SRC="xmldoc.xml" ASYNC="false"></XML> |
Is XML the solution to all the problems that have plagued the Web, or is it all just hype? It really isn't either one. XML is receiving industry-wide support for many different uses, not just the Web. It shows a lot of promise, especially if XML support spreads to different browsers. This chapter has provided only a very simple overview of XML. Find out more about XML at the W3C Web site at www.w3.org or in a good XML reference book, such as XML in Action by William J. Pardi (Microsoft Press, 1999).