2.  Formatting a Document

Suppose you have an XML document named doc1.xml that looks like this:

<event>
<description>I bought a new coffee cup!</description>
<date><year>2004</year><month>2</month><day>1</day></date>
</event>

Suppose further that you want it to look like this:

<event>
 <description>I bought a new coffee cup!</description>
 <date>
  <year>2004</year>
  <month>2</month>
  <day>1</day>
 </date>
</event>

By happy coincidence, that happens to be exactly the default output style produced by xmlformat. To reformat your document, all you have to do is run xmlformat with the document filename as the argument, saving the output in another file:

% xmlformat doc1.xml > output

Note: % represents your shell prompt; do not type it as part of the command.

If you are confident that the output style produced by xmlformat will be as you desire, you can be reckless and perform an in-place conversion:

% xmlformat -i doc1.xml

In this case, xmlformat reads the document from the input file, reformats it, and writes it back out to the same file, replacing the file's original contents. If you are not quite so reckless, use -i in conjunction with a -b option to make a backup file that contains the original document. -b takes an argument that specifies the suffix to add to the original filename to create the backup filename. For example, to back up the original doc1.xml file in a file named doc1.xml.bak, use this command:

% xmlformat -i -b .bak doc1.xml