XML


Unlike HTML, in XML documents, you're free to create your own tagsyou're not restricted to the tags that an HTML browser already understands, such as <P> and <INPUT> . That's both liberating and constrictingalthough you're now free to structure your own documents from top to bottom, it's also true that browsers such as the Internet Explorer will have no idea what to do with the elements in your XML document. The browser can't know, for example, that you mean the tag <RED> to indicate red textnot unless you handle the document in code with JavaScript (or transform the document into HTML using XSLT and JavaScript as we'll do later in the chapter). To really work with XML documents in the Internet Explorer, therefore, you have to use a scripting language such as JavaScript to interpret the XML and work with it.

Let's take a look at an XML document to get us started. You start off such documents with the XML declaration <?xml version="1.0"?> , and all the rest of the elements in the document must be enclosed in a single element, called the document element . In HTML, the document element is <HTML> ; but in XML, that element can be anything you want. Here's what our example XML document looks likeI'll make this part of a publicist's schedule book, keeping track of various publicity events and who went to them:

 <?xml version="1.0"?>  <EVENTS>      <EVENT TYPE="informal">         <EVENT_TITLE>15th Award Ceremony</EVENT_TITLE>         <EVENT_NUMBER>1207</EVENT_NUMBER>         <SUBJECT>Gala Event</SUBJECT>         <DATE>7/4/2003</DATE>         <PEOPLE>             <PERSON ATTENDENCE="present">                 <FIRST_NAME>Sam</FIRST_NAME>                 <LAST_NAME>Edwards</LAST_NAME>             </PERSON>             <PERSON ATTENDENCE="absent">                 <FIRST_NAME>Sally</FIRST_NAME>                 <LAST_NAME>Jackson</LAST_NAME>             </PERSON>             <PERSON ATTENDENCE="present">                 <FIRST_NAME>Cary</FIRST_NAME>                  <LAST_NAME>Grant</LAST_NAME>              </PERSON>          </PEOPLE>      </EVENT>  </EVENTS> 

Note that this looks very much like an HTML document, with elements that can enclose other elements. When the nesting of elements is correct (for example, no element overlaps another element like this <ELEM1><ELEM2>text</ELEM1></ELEM2> ), the document is called well- formed .

Besides being well-formed, the other usual requirement for XML documents is that they be valid . Because you can create the entire tag syntax for an XML document, the browser you're displaying it in can't check that syntax as it can for an HTML documentunless you tell the browser what that syntax is. You can tell the browser, for example, what elements can contain what other elements, what order elements must appear in, what elements can have which attributes, and so on. There are several ways to do that, and we'll look at the most common way hereusing a DTD. Using a DTD, the browser can check your XML document and make sure that it adheres to the syntax rules you've specified. We'll see how the Internet Explorer can do this near the end of this chapter.

The details of setting up a DTD are involved and beyond the scope of this book, but here's a DTD I'll add to our sample XML document which accurately defines its syntax:

(Listing 22-01.xml on the web site)
 <?xml version="1.0"?>  <!DOCTYPE EVENTS [   <!ELEMENT EVENTS (EVENT*)>   <!ELEMENT EVENT (EVENT_TITLE, EVENT_NUMBER, SUBJECT, DATE, PEOPLE*)>   <!ELEMENT EVENT_TITLE (#PCDATA)>   <!ELEMENT EVENT_NUMBER (#PCDATA)>   <!ELEMENT SUBJECT (#PCDATA)>   <!ELEMENT DATE (#PCDATA)>   <!ELEMENT FIRST_NAME (#PCDATA)>   <!ELEMENT LAST_NAME (#PCDATA)>   <!ELEMENT PEOPLE (PERSON*)>   <!ELEMENT PERSON (FIRST_NAME,LAST_NAME)>   <!ATTLIST EVENT   TYPE CDATA #IMPLIED>   <!ATTLIST PERSON   ATTENDENCE CDATA #IMPLIED>   ]>  <EVENTS>      <EVENT TYPE="informal">         <EVENT_TITLE>15th Award Ceremony</EVENT_TITLE>         <EVENT_NUMBER>1207</EVENT_NUMBER>          <SUBJECT>Gala Event</SUBJECT>         <DATE>7/4/2003</DATE>         <PEOPLE>             <PERSON ATTENDENCE="present">                 <FIRST_NAME>Sam</FIRST_NAME>                 <LAST_NAME>Edwards</LAST_NAME>             </PERSON>             <PERSON ATTENDENCE="absent">                 <FIRST_NAME>Sally</FIRST_NAME>                 <LAST_NAME>Jackson</LAST_NAME>             </PERSON>             <PERSON ATTENDENCE="present">                 <FIRST_NAME>Cary</FIRST_NAME>                 <LAST_NAME>Grant</LAST_NAME>             </PERSON>         </PEOPLE>     </EVENT>  </EVENTS> 

In fact, the Internet Explorer enables you to view such XML documents directly, as you can see in Figure 22.1. You can click small plus (+) and minus (-) signs that appear on the left in XML documents to expand and collapse XML elements.

Figure 22.1. Displaying an XML document in the Internet Explorer.

graphics/22fig01.gif

Although it's good to see our XML document in the Internet Explorer, that's of limited utility. XML documents are meant to store data, and the first step in working with that data is to access itwhich you can do with JavaScript.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

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