Home / Articles / Adobe InDesign / Real World Adobe InDesign CS4: XML

Real World Adobe InDesign CS4: XML

Article Description

XML is as simple or complicated as you care to make it. Olav Martin Kvern and David Blatner show you how to keep it simple, at least at first.
Working with DTDs

Working with DTDs

A Document Type Definition (DTD) is a text file that contains the “rules” for an XML file. There’s nothing magical about these rules—they’re just a formal version of the things you think about as you design and lay out a document. Heading levels, for example, follow a particular order; a numbered list must have at least two list items; last name data fields in directory data do not include middle names, and so on. Laying out documents is all about applying a structure to the data in the document (in addition to making it pretty to look at).

If, as we said earlier, an XML file can contain any data, in any order, how the heck can you work with other people? Most of us have, at one point or another, had some success training the writers and editors we work with to provide text files that are reasonably close to what we want. We’ve done this by giving them a detailed set of instructions on the way that they should prepare the text. And, at least for some of us, we’ve come up with a system that produces files that we can work with.

A DTD is something like a set of instructions for preparing a text file for page layout. But it’s much more than that, and somewhat more restrictive. At their best, DTDs provide an agreed-upon set of tags and XML data structures that work seamlessly with your publication processes.

DTD Basics

Given that there are entire books larger than this one on the topic of writing DTDs, you’ll have to forgive us for not going into great detail on the care and feeding of these files. (Besides, we’re still learning how to write them ourselves.)

The following are a few ground rules, however.

  • A DTD can exist as a separate file, or it can be inside an XML file. It’s most likely you’ll want to keep your DTDs as separate files, as it makes sense to work with a small set of DTDs (one or two) and apply them to a much larger number of XML files.
  • DTDs have four basic parts (called declarations): document type, comments, elements, and attributes.
  • If a DTD is included in a file containing XML data, the DTD is enclosed within the DOCTYPE declaration. This specifies the name of the top level (or “root”) element in the XML file. If the DTD is a separate file, it does not use the DOCTYPE declaration
  • Element declarations specify the name of each element—and therefore each tag—in an XML document.
  • Attribute declarations usually contain information about the information in an element (“metadata”), but they sometimes contain other data.

A (Very Brief) DTD Primer

As we mentioned above, DTDs have four basic parts: the DOCTYPE declaration, elements, attributes, and comments. The following sections provide a a bit more detail on each part.

Document type.

If the DTD appears in a file containing XML data, the DOCTYPE declaration appears at the beginning of the file, immediately following the XML declaration. The skeleton of this type of file looks something like this (in the following, the XML element “author” is the root element):

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE author [
<!-- DTD for author -->
]>
<author>
<!-- XML data-->
</author>

If the DTD is not included in an XML file, then you can omit the DOCTYPE declaration. We’re not exactly sure why you would include the DTD with the XML file, but you have the option to do so, should you so desire.

Comments.

Once you venture beyond simple DTDs (such as the examples in this book), you’ll find that it gets difficult to keep track of all of the elements and attributes in a DTD file. When this happens, you’ll want to add a note to yourself—or to anyone else who might happen to be reading the DTD—explaining what, exactly, you’re trying to do with a specific line or section of the file. Comments are preceded by “<!--”, end with “-->”, and can be span multiple lines. Here’s an example comment:

<!-- No more than one social security number per author -->

Elements.

Elements are the heart of the DVD. They specify the structure of the data used by the DTD, and they correspond to the XML tags in InDesign. Each element declaration contains the name of the element and the data that the element contains. A simple element declaration looks like this:

<!ELEMENT first (#PCDATA)>

This declares that the element is named “first,” and that the element contains text (that’s what “PCDATA” means).

A slightly more complex element is shown below.

<!ELEMENT name (first middle? last)>

This element is named name, and is made up of three elements, “first,” “middle,” and “last.” The question mark after the element “middle” means that this element is optional. Other marks specify other things about the content of the element—see Table 14-2.

Table 14-2: Element Codes

Symbol

What it Means

+

Required, and may repeat (one or more of these elements may appear in the parent element).

?

Optional, but may not repeat (only one of these elements can appear in the parent element).

*

Optional, and may repeat.

no mark

Required, and cannot repeat.

,

elements in an element. If an element name is followed by a comma, elements following the comma must appear after the element.

|

names, it means that either or both of the elements can appear; if one of the element names is required, then one or both elements must appear

Attributes.

An attribute contains additional information about an element. Attibutes are generally used for storing metadata (again, that’s data about the data in the element, not necessarily data you want to print out—the date the element was created or updated would be examples of element attributes).

An example attribute looks like this:

<!ATTLIST address updated CDATA #IMPLIED>

Attribute declarations are lists with four parts: the element name, the attribute name, the attribute type, and whether or not it’s required. In the above example, the attribute is associated with the “address” element, the attribute is named “updated,” the attribute contains the data type CDATA (text), and it’s optional.

The attribute options for the last parameter (“required”) are:

  • #REQUIRED means the attribute is required.
  • #IMPLIED means the attribute is optional.
  • #FIXED is always followed by a value; if the attribute is missing, then it’s assumed to be this value.
  • Some default value. This is really the same as #FIXED, above, in that the value provides the default value.

An Example DTD

Given our very simple XML database example, we can construct a DTD to use for validating new versions of the database. It would look something like the following.

<!-- DTD for author -->
<!ELEMENT author (name, address, city, state, zip)>
<!ELEMENT name (first, middle, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>

Importing DTDs

To import a DTD file into an InDesign document, follow these steps (see Figure 14-26).

Figure 14.26 Importing a DTD

  1. Display the Structure view, if it’s not already visible (choose Show Structure from the Structure submenu of the View menu, or click the Show Structure button at the lower-left corner of the InDesign window).
  2. Chose Load DTD from the Structure menu. InDesign displays the Load DTD dialog box.
  3. Locate and select the DTD you want to import, then click the Open button. InDesign imports the DTD.

When you import a DTD, InDesign adds the tags in the DTD file to the list of tags in the Tags panel. The tags defined in the DTD are locked—you can change the tag’s color, but not the tag’s name.

To view the DTD, choose View DTD from the Structure menu. InDesign opens the DTD in the View DTD dialog box—something like a very simple text editor (see Figure 14-27). You can scroll through the text, and you can select and copy text out of the DTD. We’re not exactly certain how useful this feature is, though we admit that it might come in handy during an “am I losing my mind or is that XML element really messed up” moment.

Figure 14.27 Viewing a DTD

“Off the Shelf” DTDs

Before you take on the task of creating a DTD from scratch, take a look at the DTD links http://www.xml.com/pub/rg/DTD_Repositories.html—you might find one there that will work for you, or at least find a good example. If you work in the newspaper industry, you might want to take a look at NewsML and SportsML, two DTDs developed by International Press Telecommunications Council (IPTC), at http://www.newsml.org.

Validating XML

Now we get to the fun part—checking an XML file to make certain it conforms to the DTD we’ve loaded. To validate XML elements, select the element and then click the Validate button at the top of the Structure view (see Figure 14-28). To validate a specific element, select the element and choose Validate From Selected Element from the Structure menu.

Figure 14.28 Validating XML

After InDesign has validated the XML, a new pane appears at the bottom of the Structure view. In this pane, InDesign displays the result of the validation. If there are errors in the XML (relative to the DTD’s specifications), then InDesign displays the offending element names in red, and lists the errors and possible solutions—the solutions are shown in blue, and are underlined.

To apply a solution, click it. This will not always solve the problems with the XML file, and it will sometimes introduce new problems. Luckily, this action can be undone—which means that you can experiment with different solutions to find which works for you.

Real World DTDs

By the time a file gets to you, it’s already too late. It needs to be laid out, proofed, and the final version printed by the deadline—and the editors, writers, and everyone else have already gone home for the night. If there’s an error in the copy you’ve been given, you don’t have time to go through proper channels—you just have to fix it. If the text files use the wrong styles, or if the graphics are in the wrong format, you can fix them and still have a chance of meeting your deadline.

If, on the other hand you, a file doesn’t conform to the DTD you’re required to use, you have a painful choice: you can start editing the XML file itself (painful and slow for all but the simplest XML files), you can turn off DTD validation and attempt to make sense of the XML structure, or you can stick to the “letter of the law” and reject the XML file because it didn’t conform to the DTD.

Publishing is about deadlines, more deadlines, and output. Keeping a press waiting is an expensive business, as the authors know to their bitter experience. We can add all of the formal handoffs, review processes, and conform to every standard that’s ever been formulated, but, in the end, we sometimes have to throw it all out the window in order to get the pages out the door.

We’re not saying that you shouldn’t use DTDs. But we did want to caution you—if you’re going to commit to a particular DTD, make certain that you build plenty of time into your process for validation and for fixing XML files that won’t validate.

7. Transforming XML with XSLT | Next Section Previous Section