Sams Teach Yourself XML in 21 Days

Sams Teach Yourself XML in 21 Days

By Steven Holzner

Creating Tags and Elements

You give structure to the data in an XML document using elements. An XML element consists of a start tag and an end tag—except in the case of elements that are defined to be empty, which consist only of one tag—and might include character data and/or other elements. We've already seen both tags and elements in action.

Creating Tag Names

In XML 1.0, the names you give to a tag, like "message" in the tag <message>, are tightly controlled. You can start a tag name with a letter, an underscore, or a colon. The next characters might be letters, digits, underscores, hyphens, periods, and colons (but no whitespace).

In XML 1.1, things have changed. Instead of saying that everything not permitted is forbidden, XML 1.1 names are designed so that everything that is not forbidden is permitted. The idea is that because Unicode will continue to grow, further changes to XML can be avoided by allowing almost any character, including those not yet assigned, in names.

Formally speaking, in XML 1.1 you can start a name with :, A to Z, _, a to z, or the Unicode characters &#xC0; to &#x2FF;, &#x370; to #x37D;, &#x37F; to &#x1FFF;, &#x200C; to #x200D;, &#x2070; to &#x218F;, &#x2C00; to &#x2FEF;, &#x3001; to &#xD7FF;, and &#xF900; to &#xEFFFF;. This excludes -, ., and digits. The next characters in a name may include all the characters you can start a name with, as well as -, ., 0 to 9, &#xB7;, &#x0300; to &#x036F;, and &#x203F; to &#x2040;.

For example, here are some allowable XML tags:

<DOCUMENT>
<document>
<Chapter15>
<Section-19>
<_text>

Bear in mind that tag names are case sensitive, so <PUMPKIN> is not the same as <pumpkin>, which is not the same as <PuMpKiN>. Actually, your document can have <PUMPKIN>, <pumpkin>, and <PuMpKiN> tags at the same time, and they would all be considered different. Here are some tags that are not legal in XML:

<2005>
<Loan Number>
<.text>
<*yay*>
<EMPLOYEE(ID)>

So far, the elements you've seen have all contained data or other elements, but elements don't need to contain any content at all if they're empty.

Creating Empty Elements

In XML, empty elements only have one tag, not a start and end tag. You might be familiar with empty elements from HTML; for example, the HTML <img>, <li>, <hr>, and <br> elements are empty, which is to say that they do not enclose any content (either character data or markup). Empty elements are represented with only one tag (in HTML, there is no closing </img>, </li>, </hr>, and </br> tags).

In XML, you close an empty element with />, not just >. For example, if the <heading> element were an empty element, it might appear like this in an XML document:

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <heading/>
    <message>
        This is an XML document!
    </message>
</document>

Empty elements can have attributes, as in this case, where we're using an attribute named text to hold the text content of this element:

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <heading text = "Hello From XML"/>
    <message>
        This is an XML document!
    </message>
</document>

The </> syntax is XML's way of making sure that an XML processor isn't left searching for a nonexistent closing tag. In fact, in XHTML, which is the derivation of HTML in XML, the <img>, <li>, <hr>, and <br> elements are used as <img />, <li />, <hr />, and <br />, and HTML browsers don't have a problem with that.

Creating a Root Element

If you want your document to be well formed, it must have one element that contains all the other elements and text data in the document—the root element, also called the document element. In our sample XML file, the document element happens to be named <document>, although you can use any legal name.

Each well-formed XML document must contain one element that contains all the other elements, and the containing element is called the root element. The root element is a very important part of XML documents, especially when you look at them from an XML processor's point of view, because you parse XML documents starting with the root element. In ch02_01.xml, developed at the start of this chapter, the root element is the <document> element (although you can give the root element any name):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="ch01_04.css"?>
<document>
    <heading>
        Hello From XML
    </heading>
    <message>
        This is an XML document!
    </message>
</document>

Creating Attributes

XML attributes, which can appear in elements, processing instructions, and XML declarations, work much like attributes in HTML. In XML, you use them in pairs like this: attributename = "value" in opening tags. Unlike HTML, note that the values you assign to attributes must be quoted (even if they're numbers), and that if you use an attribute, it must be assigned a value. (Some HTML attributes, like BORDER, don't need to be assigned a value.) Using DTDs or XML schemas, you can make an attribute required or optional—if required, you must use the attribute when you use the corresponding element, and you must assign the attribute a value. You can also specify what values an attribute may be assigned, if you want to.

You can see an example in Listing 2.2, where we've given each <employee> element an attribute named status, and are assigning the text "retired", "active", and "leave" to that attribute in various places in the document.

Example 2.2. Using Attributes in an XML Document (ch02_02.xml)

<?xml version = "1.0" standalone="yes"?>
<document>
    <employee status="retired">
        <name>
            <lastname>Kelly</lastname>
            <firstname>Grace</firstname>
        </name>
        <hiredate>October 15, 2005</hiredate>
        <projects>
            <project>
                <product>Printer</product>
                <id>111</id>
                <price>$111.00</price>
            </project>
            <project>
                <product>Laptop</product>
                <id>222</id>
                <price>$989.00</price>
            </project>
        </projects>
    </employee>
    <employee status="active">
        <name>
            <lastname>Grant</lastname>
            <firstname>Cary</firstname>
        </name>
        <hiredate>October 20, 2005</hiredate>
        <projects>
            <project>
                <product>Desktop</product>
                <id>333</id>
                <price>$2995.00</price>
            </project>
            <project>
                <product>Scanner</product>
                <id>444</id>
                <price>$200.00</price>
            </project>
        </projects>
    </employee>
    <employee status="leave">
        <name>
            <lastname>Gable</lastname>
            <firstname>Clark</firstname>
        </name>
        <hiredate>October 25, 2005</hiredate>
        <projects>
            <project>
                <product>Keyboard</product>
                <id>555</id>
                <price>$129.00</price>
            </project>
            <project>
                <product>Mouse</product>
                <id>666</id>
                <price>$25.00</price>
            </project>
        </projects>
    </employee>
</document>

You can see this XML document in Internet Explorer, including the attributes and their values, in Figure 2.10.

02fig10.gif

Figure 2.10 Viewing element attributes in Internet Explorer.

Just like the data in an element, an XML processor can retrieve the values you've assigned to an element's attributes. We'll see how to do that in both JavaScript and Java later in this book.

Attributes hold data, and elements hold data—so when should you use which? It's up to you, but practically speaking, there are two things to take into account. The first is that you can't specify document structure using attributes. For example, this <employee> element makes it clear what data you're storing about an employee:

<employee status="retired">
    <name>
        <lastname>Kelly</lastname>
        <firstname>Grace</firstname>
    </name>
    <hiredate>October 15, 2005</hiredate>
    <projects>
        <project>
            <product>Printer</product>
            <id>111</id>
            <price>$111.00</price>
        </project>
        <project>
            <product>Laptop</product>
            <id>222</id>
            <price>$989.00</price>
        </project>
    </projects>
</employee>

A good rule to follow, therefore, is to use elements to structure your document, and to use attributes when you have more information to include about a specific element, as when you want to indicate the language the enclosed text is in. Here's an example where we're storing the standard abbreviation for U.S. English, "en-US", in an attribute:


   <text language="en-US">
It was a dark and stormy night. A shot rang out!
.
.
.
</text>

Also, it's worth noting that using too many attributes can make a document hard to read, something you'll readily see if you start converting the earlier <employee> element to use attributes rather than subelements to hold its data:

<employee status="retired">
    <name lastname="Kelly" firstname="Grace"/>
    <hiredate>October 15, 2005</hiredate>
    <projects>
        <project product="Printer" id="111" price="$111.00"/>
            .
            .
            .

Naming Attributes

In XML, attribute names must follow the same rules as those for element names. That means in XML 1.0 you can start an attribute name with a letter, an underscore, or a colon, and the next characters may be letters, digits, underscores, hyphens, periods, and colons (but no whitespace). In XML 1.1, you follow the rules for XML 1.1 names, as discussed earlier today.

Here are some legal attribute name examples:

<brush width="10" height="5" color="cyan"/>
<point x="10" y="100"/>
<book title="My Sweet Summer" review="Yuck!"/>
<vegetable name="broccoli" color="green"/>

Here are some attribute names that are not legal:

<fish measured length="500"/>
<friend 1stPhone="555.2222" 2ndPhone="555.3333"/>
<application .NET="yes"/>
<person name(or nick name)="sammy"/>

Assigning Values to Attributes

As noted, all data in XML documents is text, including the data you assign to attributes. Even when you assign a number to an attribute, you treat that number as if it were text:

<constant name="pi" value="3.1415926"/>

You can use single or double quotation marks when quoting an attribute's value. By convention, double quotation marks are usually used, but if the value you're quoting contains double quotation marks—for example, He said, "No worries."—you can't just surround that value with double quotation marks, because the XML processor won't understand where the quotation begins and ends. Instead, you can use single quotation marks to begin and end the attribute's value like this:

<citation text='He said, "No worries."' />

What if the attribute value contains both single and double quotes, as when you want to say The tree was 16' 3" tall? In this case, you can use the XML-defined general entity references for a single quotation mark, &apos; and for a double quotation mark, &quot;, like this:

<citation text="The tree was 16&apos; 3&quot; tall" />

The XML processor will turn this back into The tree was 16' 3" tall when it parses this text.

Specifying Language with the xml:lang Attribute

Besides xml:space, there's one more attribute that comes built into XMLxml:lang, which lets you specify the language of a document, such as English, German, and so on. Although xml:space and xml:lang are "built into" XML, and so should be usable with any element, some XML processors will not support these attributes.

You can set the xml:lang attribute to these values:

Here is an example; in this case, we're specifying that the language of an element should be English, using the xml:lang attribute and the ISO language code "en":

<p xml:lang="en">The quick brown fox jumped over the lazy dog.</p>

Besides specifying the language, you can also specify a language subcode to indicate a regional variation or dialect, such as U.S. English. These subcodes are two characters each, and they're also defined by the International Organization for Standardization in the document ISO 3166-1:1997, "Codes for the Representation of Names of Countries and Their Subdivisions—Part 1: Country Codes." For example, here's how you might specify that one element holds British English content, and one American English:

<p xml:lang="en-GB">What colour is the sky?</p>
<p xml:lang="en-US">What color is the sky?</p>

Note that xml:lang specifies the language used in both the element's content (including all text data, if you use xml:lang in the document element), as well as an element's attribute values, as here, where we're using German in an element's attributes:

<p farbe="weiss" xml:lang="de">

Share ThisShare This

Informit Network