XML technologies / XML / Perl & XML / Introduction to XML / Processing instructions and other markup
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
Besides elements, other syntax helps process XML. The processing instructions (PIs), or processing instructions, control a particular XML processor. A processing instruction has a parameter target identifying the processor, followed by optional data. Unfamiliar programs ignore these tags. Here is a real example from hacking an O’Reilly book:
<?file-breaker start chap04.xml?><chapter>
<title>Ein langer Titel,<?lb?>der gar kein Ende zu nehmen scheint</title>
<?xml2pdf vspace 10pt?> The first PI’s target is file-breaker and its data is chap04.xml. Programs looking for that target react to the data, here creating a file for the following XML.
The second PI has only a target, lb. We have seen it used to force a line break, but this is problematic. It replaces whitespace: programs ignoring it join two words, fixable by adding a space before or after it. Also the target is an instruction, better placed in data, rather than a program name. A specific name like the next PI’s (xml2pdf) is preferable.
PIs are convenient. Target and data are unrestricted except that targets should be specific and data short.
Those using Perl’s built-in POD markup, Plain Old Documentation (Note: see Chapter 26 of Programming Perl or the manpage perlpod), may notice similarities with POD directives, notably =for sections and =begin/=end (blocks). These let you send short targeted messages with arguments, or arbitrary text, to a specific POD processor.
Another useful object is the XML comment. The processor ignores its text completely; only people read it. Authors leave reminders for themselves or colleagues, or temporarily hide a section—a useful trick when debugging an XML document without knowing the troublesome part. For example:
<!-- Dieser Text ist für den Parser unsichtbar. -->
Dieser Satz dagegen enthält absolut sichtbare XML-Textdaten.
<!--
<para>Dieser Absatz war früher einmal ein Teil des Dokuments - jetzt ist er
es nicht mehr.</para>
-->These look and work like HTML comments.
Comments cannot contain other comments. Even the string -- is forbidden because the parser would treat it as a comment ending.
Finally consider a CDATA section. CDATA means Character Data, text that the XML parser does not interpret as markup. It treats the entire section as text even if it looks like markup. This is useful with repeated characters such as <, > and &. Entity references would work too but become cumbersome.
For example:
<codelisting>
<![CDATA[if( $val > 3 && @lines ) {
$input = <FILE>;
}]]>
</codelisting> Everything between <![CDATA[ and ]]> is plain text, so markup characters may be used freely. We rarely use CDATA because it looks cluttered and complicates XML processing, but do not let us discourage you. (Note: in fact we used CDATA throughout the DocBook-inspired XML of this book. All source code and XML examples use CDATA to avoid references for < and & .)
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top