Appendix A. Exercise Solutions

This appendix contains some suggested solutions to the exercise questions posed at the end of most of the chapters throughout the book.

This chapter gave an overview of XML and why it's so useful.

Question 1

Modify the <name> XML document you've been working with to include the person's title (Mr., Ms., Dr., and so on).

Solution

Because of the self-describing nature of XML, the only difficult part of adding a title to the <name> example is deciding what to call it. If you call it <title>, you can add it to your document as follows:

<name>
       <title>Mr.</title>
       <first>John</first>
       <middle>Fitzgerald Johansen</middle>
       <last>Doe</last>
</name>

Another possibility is to treat a title as a simple prefix to the name; this also allows you the flexibility of adding a suffix. This approach might look like the following:

<name>
      <prefix>Mr.</prefix>
      <first>John</first>
      <middle>Fitzgerald Johansen</middle>
      <last>Doe</last>
      <suffix>the 3rd</suffix>
</name>

In this case, instead of giving the data an explicit label, you're making it more generic and allowing some text to come before and after the name.

Question 2

The <name> example we've been using so far has been in English, but XML is language-agnostic, so you can create XML documents in any language you wish. Create a new French document type to represent a name. You can use the following table for the names of the XML elements:

English

French

name

identité

first

prénom

Last

nom

middle

deuxième-prénom

Solution

Although this might ...

Get Beginning XML, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.