XML is intended to be human-readable , so a common example effectively illustrates its primary features. Consider the possibility of an electronic business card. Every day people exchange physical business cards that have a common structure. XML makes it easy to do this electronically . The document in Example 2-1a contains much of the data found on a business card. Example 2-1a<BusinessCard> <Name> <GivenName>Kevin</GivenName> <MiddleName>Stewart</MiddleName> <FamilyName>Dick</FamilyName > </Name> <Title> Software Technology Analyst </Title> <Author/> <ContactMethods> <Phone>650-555-5000</Phone> <Phone>650-555-5001</Phone> </ContactMethods> </BusinessCard> As you can see, tags in XML documents are similar to those in HTML documents. Familiar open and close tags define the beginnings and ends of elements . These elements appear in a stricthierarchy: "GivenName" is a child of "Name," which is a child of "BusinessCard." The most important difference from HTML is that you can use any types of elements you choose rather than relying on a predefined set. DTDs describe the allowable structure of XML documents. A document does not need a DTD, but using a DTD is a convenient way for two parties to ensure that they are using the same data format. A DTD can constrain the types of data that may occur in a document, the hierarchy of data items, and the number of times each item of data may appear. Example 2-1b shows a DTD for our simplified business card document. Example 2-1b<!ELEMENT BusinessCard (Name, Title, Author?, ContactMethods)> <!ELEMENT Name (GivenName, MiddleName?, FamilyName)> <!ELEMENT GivenName (#PCDATA)> <!ELEMENT MiddleName (#PCDATA)> <!ELEMENT FamilyName (#PCDATA)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author EMPTY> <!ELEMENT ContactMethod (Phone*)> <!ELEMENT Phone (#PCDATA)> A DTD lists the types of elements within a document, the types of child elements for these elements, and so on. Special characters such as the "?" and the "*" constrain the number of times an element may appear ”in this case, 0 or 1 times and 0 or more times, respectively. So our DTD says that a business card may have 0 or 1 "Author" elements, 0 or 1 "MiddleName" elements, and 0 or more "Phone" elements. |