Section 7.1. XML Basics


7.1. XML Basics

Here is a very simple XML document that is marked up with tags I made up to describe the liner notes for my famous end-of-the-year music compilations. (I could call it JenML).

 <?xml version="1.0"?> <compilation > <title>Oh Baby! Jen's Favorites</title> <year>2005</year> <image source="ohbabycover.jpg"/> <tracklist>   <track number="1">      <artist>The Wrens</artist>      <song>         <song_title>Hopeless</song_title> from         <album_title>The Meadowlands</album_title>,         <label>Absolutely Kosher Records</label>,         <release_date>2003</release_date>      </song>      <comments>I love The Wrens, both musically and personally.</comments>   </track> <!--more tracks added here --> </tracklist> </compilation> 

Certain things about this example should look familiar to anyone who has seen an HTML document. First, it is a plain-text document. As such, it can be created or edited in any text editor. It also uses tags in brackets to indicate the start and end of content elements in the document. Consider this element from the example:

 <artist>The Wrens</artist> 

The element includes the content (in this case, the character data "The Wrens") and its markup (the start tag <artist> and end tag </artist>). In XML, tags are case-sensitive, so <ARTIST>, <Artist>, and <artist>would be parsed as three different elements. Elements may contain character data, other elements, or both. Some elements are empty, which means they have no content. <meta/> is an example of an empty element in XHTML. Elements may be clarified or enhanced with attributes that provide extra information about that element. In the example, the image element uses the source attribute to provide the location of the image file.

7.1.1. Meaningful Markup

The most significant thing to note here is that the tags describe the information they contain in a meaningful way. In XML, element names are intended to be simple, descriptive, and easily readable by human beings as well as machines. Notice also that the tags do not provide any indication of how the document should look when it is displayed. Their purpose is to provide a semantic description (the meaning) of their contents. XML documents rely on style sheets to handle all matters of presentation.

Together, the elements in a document create its structure. Notice in the example that some elements contain other elements, which may contain yet more elements. This hierarchy is referred to as the document tree . It starts with a root element (compilation in the example) and branches out in layers of parent/child relationships. Every XML document must have exactly one root element, and the root element has no ancestors. Document structure is covered in more detail in Chapter 16.

The concepts of semantic markup and document structure are directly relevant to web design. HTML and XHTML are markup languages for describing text documents whose "data" consists of paragraphs, headings, lists, and so on. In proper HTML markup, elements should accurately describe their contents, and should not be chosen to achieve a particular visual effect in a browser. Additionally, an awareness of a document's structure will be a major advantage when planning and writing style sheets.


7.1.2. Text as Data, Data as Text

It is easy to see even from our simple example how XML markup treats content in a text document like data. So while this document can be displayed in a page format, it can just as easily be stored in a database (which is a common use of XML-formatted information).

On the flip side, XML allows data to be stored in a plain-text format. This is the key to XML's rampant success in the computing world. Data that had previously been stored in proprietary, device-specific formats can now be marked up in a text file and shared between incompatible systems. Longevity is improved as well. XML documents are self-defining, intuitive, and not tied to a format or system that may grow obsolete.




Web Design in a Nutshell
Web Design in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009879
EAN: 2147483647
Year: 2006
Pages: 325

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