O'Reilly logo

Beginning XML, 4th Edition by Andrew Watt, David Hunter, Linda McKinnon, Joe Fawcett, Eric van der Vlist, Jon Duckett, Danny Ayers, Jeff Rafter

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required