1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
|
<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
>
<article>
<title>
<command>xmlformat</command> Tutorial
</title>
<articleinfo>
<author>
<surname>DuBois</surname>
<firstname>Paul</firstname>
<email>paul@kitebird.com</email>
</author>
</articleinfo>
<sect1 id="introduction">
<title>
Introduction
</title>
<para>
This document is a user guide that provides a tutorial introduction to
the <command>xmlformat</command> program. Another document,
<citetitle>The <command>xmlformat</command> Document
Formatter</citetitle>, describes the capabilities of
<command>xmlformat</command> in more detail.
</para>
</sect1>
<sect1 id="formatting-document">
<title>
Formatting a Document
</title>
<para>
Suppose you have an XML document named <filename>doc1.xml</filename>
that looks like this:
</para>
<!-- xmlize -t screen -C examples/doc1.xml -->
<screen>
<![CDATA[<event>
<description>I bought a new coffee cup!</description>
<date><year>2004</year><month>2</month><day>1</day></date>
</event>]]>
</screen>
<para>
Suppose further that you want it to look like this:
</para>
<!-- ../xmlformat.rb -f /dev/null examples/doc1.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<event>
<description>I bought a new coffee cup!</description>
<date>
<year>2004</year>
<month>2</month>
<day>1</day>
</date>
</event>]]>
</screen>
<para>
By happy coincidence, that happens to be exactly the default output
style produced by <command>xmlformat</command>. To reformat your
document, all you have to do is run <command>xmlformat</command> with
the document filename as the argument, saving the output in another
file:
</para>
<screen>
% <userinput>xmlformat doc1.xml > output</userinput>
</screen>
<para>
Note: <literal>%</literal> represents your shell prompt; do not type it
as part of the command.
</para>
<para>
If you are confident that the output style produced by
<command>xmlformat</command> will be as you desire, you can be reckless
and perform an in-place conversion:
</para>
<screen>
% <userinput>xmlformat -i doc1.xml</userinput>
</screen>
<para>
In this case, <command>xmlformat</command> 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 <option>-i</option> in conjunction with a
<option>-b</option> option to make a backup file that contains the
original document. <option>-b</option> 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
<filename>doc1.xml</filename> file in a file named
<filename>doc1.xml.bak</filename>, use this command:
</para>
<screen>
% <userinput>xmlformat -i -b .bak doc1.xml</userinput>
</screen>
</sect1>
<sect1 id="using-config-file">
<title>
Using a Configuration File
</title>
<para>
In the preceding example, the desired output style for
<filename>doc1.xml</filename> was the same as what
<command>xmlformat</command> produces by default. But what if the
default style is <emphasis>not</emphasis> what you want? In that case,
you must tell <command>xmlformat</command> how to handle your document.
This is at once both the weakness and strength of
<command>xmlformat</command>. The weakness is that it is extra work to
instruct <command>xmlformat</command> how you want it to format a
document. The strength is that it's possible to do so. Other XML
formatters do not require any extra work, but that's because they are
not configurable.
</para>
<para>
Suppose <filename>doc2.xml</filename> looks like this:
</para>
<!-- xmlize -t screen -C examples/doc2.xml -->
<screen>
<![CDATA[<example><title>Compiling and Running a Program</title>
<para>To compile and run the program,
use the following commands, where
<replaceable>source-file</replaceable>
is the name of the source file:</para><screen>
<userinput>cc</userinput> <replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
That's ugly, and you want it to rewrite it like this:
</para>
<!-- ../xmlformat.rb -f examples/doc2.conf4 examples/doc2.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<example>
<title>Compiling and Running a Program</title>
<para>
To compile and run the program, use the following commands,
where <replaceable>source-file</replaceable> is the name of
the source file:
</para>
<screen>
<userinput>cc</userinput> <replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
The key characteristics of this rewrite are as follows:
</para>
<itemizedlist>
<listitem><para>
Child elements of the <literal><example></literal> element are
separated by blank lines, but not indented within it.
</para></listitem>
<listitem><para>
The text inside the <literal><para></literal> element is
reformatted, adjusted to 60 characters per line and indented.
</para></listitem>
<listitem><para>
The contents of the <literal><screen></literal> element are left
alone.
</para></listitem>
</itemizedlist>
<para>
Unfortunately, if you run <filename>doc2.xml</filename> through
<command>xmlformat</command>, it comes out like this:
</para>
<!-- ../xmlformat.rb -f /dev/null examples/doc2.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<example>
<title>Compiling and Running a Program</title>
<para>To compile and run the program,
use the following commands, where
<replaceable>source-file</replaceable>
is the name of the source file:</para>
<screen>
<userinput>cc</userinput>
<replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
This output is unsuitable. Among the offenses committed by
<command>xmlformat</command>, two are most notable:
</para>
<itemizedlist>
<listitem><para>
The text of the <literal><para></literal> element has been left
alone, not reformatted.
</para></listitem>
<listitem><para>
The <literal><screen></literal> element content has been
reformatted, not left intact.
</para></listitem>
</itemizedlist>
<para>
In these respects, it appears that <command>xmlformat</command> has done
exactly the <emphasis>opposite</emphasis> of what was wanted!
Furthermore, had you used the <option>-i</option> option to reformat the
file in place without using <option>-b</option> to make a backup, at
this point you would have a file containing a
<literal><screen></literal> element that you'd have to fix up by
hand to restore it to its original condition.
</para>
<para>
What a worthless, worthless program!
</para>
<para>
The rewriting of the <literal><screen></literal> element points to
an important lesson: Before trusting <command>xmlformat</command> with
your documents, it's best to run some tests and tune your configuration
as necessary to make sure it will produce the results you want.
Otherwise, you may produce changes that affect the integrity of your
documents. This is particularly true when they contain elements such as
<literal><screen></literal> or
<literal><programlisting></literal> that should be copied
verbatim, without change.
</para>
<para>
Configuring <command>xmlformat</command> amounts to writing a
configuration file that instructs it what to do. For
<filename>doc2.xml</filename>, that means telling
<command>xmlformat</command> to leave the
<literal><screen></literal> element alone, to normalize the text
of the paragraph to fill lines and wrap them to a given length, and to
put blank lines around sub-elements of the
<literal><example></literal> element.
</para>
<para>
Let's begin by creating a very basic configuration file. What should we
call it? <command>xmlformat</command> can read configuration settings
from a file named on the command line with a <option>-f</option> or
<option>--config-file</option> option. This means you can name the file
whatever you want. However, if you put the settings in a file named
<filename>xmlformat.conf</filename> in the current directory,
<command>xmlformat</command> will read the file automatically. That's an
easier approach, because you won't need to use a command-line option to
specify the configuration file. So create a file named
<filename>xmlformat.conf</filename> that contains the following two
lines:
</para>
<screen>
screen
format = verbatim
</screen>
<para>
These lines specify that <literal><screen></literal> elements
should be formatted as verbatim elements. That is,
<command>xmlformat</command> should reproduce their content in the
output exactly as it appears in the input, without modification. The
first line must begin in column 1 (no preceding spaces or tabs). The
second line must begin with at least one space or tab. Presence or
absence of whitespace is how <command>xmlformat</command> distinguish
the names of elements to be formatted from the instructions that
indicate <emphasis>how</emphasis> to format them.
</para>
<para>
After creating <filename>xmlformat.conf</filename>, run
<command>xmlformat</command> again to process
<filename>doc2.xml</filename>. It reads the newly created configuration
file and produces this result:
</para>
<!-- ../xmlformat.rb -f examples/doc2.conf1 examples/doc2.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<example>
<title>Compiling and Running a Program</title>
<para>To compile and run the program,
use the following commands, where
<replaceable>source-file</replaceable>
is the name of the source file:</para>
<screen>
<userinput>cc</userinput> <replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
That's a little better: <command>xmlformat</command> has not destroyed
the <literal><screen></literal> element by reformatting it. But
problems remain: The paragraph content has not been reformatted, and
there are no blank lines between sub-elements.
</para>
<para>
Let's take care of the paragraph next. To set up its formatting, add a
section to <filename>xmlformat.conf</filename> for
<literal><para></literal> elements:
</para>
<screen>
para
format = block
normalize = yes
wrap-length = 60
subindent = 1
screen
format = verbatim
</screen>
<para>
The order of sections in the configuration file doesn't matter. Put them
in the order that makes most sense to you. The order of option lines
under the initial section line doesn't matter, either.
</para>
<para>
The first two options in the <literal>para</literal> section specify
that the <literal><para></literal> element is a block element, and
that text within it should be normalized. Turning on the
<literal>normalize</literal> option tells <command>xmlformat</command>
that it's okay to reformat the text within the element. This means that
runs of whitespace within the text are collapsed to single spaces, and
that whitespace at the beginning and end of the text can be adjusted
(typically to put the text on different lines than the element's opening
and closing tags). Enabling normalization also allows you to perform
text line-wrapping and indenting. The <literal>wrap-length</literal>
option specifies the maximum number of characters per line, and
<literal>subindent</literal> specifies the indenting of text and
sub-elements, relative to the element's own tags. Note that when
<command>xmlformat</command> performs line-wrapping, it includes the
currently prevailing indent as part of the line length. (For example, if
the prevailing indent is 20 spaces and <literal>wrap-length</literal>
value is <literal>60</literal>, lines will contain at most 40 characters
following the indentation.)
</para>
<para>
After adding the <literal>para</literal> section to
<filename>xmlformat.conf</filename>, <command>xmlformat</command>
produces this result:
</para>
<!-- ../xmlformat.rb -f examples/doc2.conf2 examples/doc2.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<example>
<title>Compiling and Running a Program</title>
<para>
To compile and run the program, use the following
commands, where
<replaceable>source-file</replaceable>
is the name of the source file:
</para>
<screen>
<userinput>cc</userinput> <replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
The paragraph now is wrapped and indented. However, it doesn't seem to
be wrapped <emphasis>quite</emphasis> correctly, because the
<literal><replaceable></literal> element actually would fit on the
previous line. This happens because no formatting options were specified
for <literal><replaceable></literal> in the configuration file. As
a result, it is treated as having the default element type of
<literal>block</literal>, using the default behavior that block elements
are written out beginning on a new line.
</para>
<para>
To fix this problem, we should configure
<literal><replaceable></literal> as an inline element. That will
cause it to be formatted inline with the other text (and thus
line-wrapped along with it). Modify the configuration file to include a
<literal>replaceable</literal> section: this:
</para>
<screen>
para
format = block
normalize = yes
wrap-length = 60
subindent = 1
replaceable
format = inline
screen
format = verbatim
</screen>
<para>
The resulting output after making this change is as follows:
</para>
<!-- ../xmlformat.rb -f examples/doc2.conf3 examples/doc2.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<example>
<title>Compiling and Running a Program</title>
<para>
To compile and run the program, use the following
commands, where <replaceable>source-file</replaceable> is
the name of the source file:
</para>
<screen>
<userinput>cc</userinput> <replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
We're getting close now. All we need to do is space out the
<literal><example></literal> child elements with a blank line in
between. Sub-element spacing is controlled by three formatting
properties:
</para>
<itemizedlist>
<listitem><para>
<literal>entry-break</literal> controls spacing after the opening tag of
an element (that is, the spacing upon entry into the element's content).
</para></listitem>
<listitem><para>
<literal>element-break</literal> controls the spacing between
sub-elements.
</para></listitem>
<listitem><para>
<literal>exit-break</literal> controls spacing before the closing tag of
an element (that is, the spacing upon exit from the element's content).
</para></listitem>
</itemizedlist>
<para>
The value for each of these formatting options should be an integer
indicating the number of newlines to write. A value of
<literal>1</literal> causes one newline, which acts simply to break to
the next line. To get a blank line, the break value needs to be
<literal>2</literal>. Modify the configuration file by adding a section
for <literal><example></literal> elements:
</para>
<screen>
example
format = block
entry-break = 2
element-break = 2
exit-break = 2
subindent = 0
para
format = block
normalize = yes
wrap-length = 60
subindent = 1
replaceable
format = inline
screen
format = verbatim
</screen>
<para>
The resulting output is:
</para>
<!-- ../xmlformat.rb -f examples/doc2.conf4 examples/doc2.xml | xmlize -t screen -C -->
<screen>
<![CDATA[<example>
<title>Compiling and Running a Program</title>
<para>
To compile and run the program, use the following commands,
where <replaceable>source-file</replaceable> is the name of
the source file:
</para>
<screen>
<userinput>cc</userinput> <replaceable>source-file</replaceable>
<userinput>./a.out</userinput>
</screen>
</example>]]>
</screen>
<para>
We're done!
</para>
<para>
You may be thinking, "Wow, that's a lot of messing around just to format
that tiny little document." That's true. However, the effort of setting
up configuration files tends to be "reusable," in the sense that you can
use the same file to format multiple documents that all should be
written using the same style. Also, if you have different projects
requiring different styles, it tends to be easiest to begin setting up
the configuration file for one project by beginning with a copy of the
file from another project.
</para>
</sect1>
<sect1 id="inherited-formatting">
<title>
Discovering "Inherited" Formatting Options
</title>
<para>
In the final formatting of <filename>doc2.xml</filename>, note that the
paragraph tags appear on separate lines preceding and following the
paragraph content. This occurs despite the fact that the configuration
file specifies no break values in the <literal>para</literal> section,
because if you omit formatting options for an element, it "inherits" the
default properties. In the case of the <literal><para></literal>
element, the relevant unspecified properties are the
<literal>entry-break</literal> and <literal>exit-break</literal> values.
For block elements, both have a value of <literal>1</literal> by default
(that is, one newline), which causes a line break after the opening tag
and before the closing tag.
</para>
<para>
If you want to see all the formatting options
<command>xmlformat</command> will use, run it with the
<option>--show-config</option> option. For example:
</para>
<screen>
% <userinput>xmlformat --show-config</userinput>
*DEFAULT
format = block
entry-break = 1
element-break = 1
exit-break = 1
subindent = 1
normalize = no
wrap-length = 0
*DOCUMENT
format = block
entry-break = 0
element-break = 1
exit-break = 1
subindent = 0
normalize = no
wrap-length = 0
example
format = block
entry-break = 2
element-break = 2
exit-break = 2
subindent = 0
normalize = no
wrap-length = 0
para
format = block
entry-break = 1
element-break = 1
exit-break = 1
subindent = 1
normalize = yes
wrap-length = 60
replaceable
format = inline
screen
format = verbatim
</screen>
<para>
No configuration file is specified on the command line, so
<command>xmlformat</command> reads the default configuration file,
<filename>xmlformat.conf</filename>. Then it displays the resulting
configuration options. You can see that the <literal>para</literal>
section has inherited break values from the <literal>*DEFAULT</literal>
section.
</para>
</sect1>
<sect1 id="unconfigured-elements">
<title>
Checking for Unconfigured Elements
</title>
<para>
Any elements appearing in the input document that are not named in the
configuration file are formatted using the values of the
<literal>*DEFAULT</literal> section. If the file contains no
<literal>*DEFAULT</literal> section, <command>xmlformat</command> uses
built-in default values.
</para>
<para>
If you want to see whether there are any elements in the document for
which you haven't specified any formatting options, run
<command>xmlformat</command> with the
<option>--show-unconfigured-elements</option> option. For example:
</para>
<screen>
% <userinput>xmlformat --show-unconfigured-elements doc2.xml</userinput>
The following document elements were assigned no formatting options:
title
</screen>
<para>
As it happens, the title already formats in the desired fashion, so
there's no necessity of adding anything more to the configuration file.
</para>
</sect1>
</article>
|