XML technologies / XML / XML in a Nutshell / XML basics / Processing instructions
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
In HTML comments are sometimes misused to support nonstandard extensions. For example, the content of the element script is sometimes enclosed in a comment to hide it from browsers without scripting. Apache parses comments in .shtml files for server-side extensions. Unfortunately, comments and their semantics seldom survive passage through HTML editors and processors. Worse, an innocent comment may be misinterpreted as application input.
XML provides processing instructions (English: Processing instructions) as another way to pass information to specific applications reading the document. A processing instruction starts with <? and ends with ?>. Immediately after <? comes an XML name, the target, perhaps the intended application’s name or simply an identifier for this instruction. The remaining text uses a format understood by its intended applications.
For example, HTML uses a robots-META tag to tell search engines and other agents whether and how to index a page. This processing instruction was proposed as an XML equivalent:
<?robots index="yes" follow="no"?> This instruction’s target is robots. Its syntax consists of two pseudo-attributes named index and follow, with values yes or no. Its semantics say that if the attribute index has value yes, search agents should index the page. If index has value no, no agent should index it. Similarly, links are followed if follow has value yes. If it has value no, they are not.
Other processing instructions may have entirely different syntax and semantics. They can, for example, contain virtually unlimited text. PHP embeds large programs in processing instructions. For example:
<?php
mysql_connect("database.unc.edu", "clerk", "password") ;
$result = mysql("HR", "SELECT LastName, FirstName FROM Employees
ORDER BY LastName, FirstName") ;
$i = 0;
while ($i < mysql_numrows ($result)) {
$fields = mysql_fetch_row($result);
echo "<person>$fields[1] $fields[0] </person>\r\n";
$i++;
}
mysql_close( );
?>Processing instructions are markup, but not elements. Like comments, they may appear anywhere outside tags, including before and after the root element. The most common processing instruction, xml-stylesheet, associates documents with stylesheets. It always precedes the root element, as the following example shows. Here, the instruction xml-stylesheet tells the browser to apply the CSS stylesheet person.css to this document before displaying it.
<?xml-stylesheet href="person.css" type="text/css"?>
<person>
Alan Turing
</person>Code example: An XML document with a processing instruction in its prolog
The letter sequence xml is forbidden as a processing instruction name in any case combination, such as XML or Xml, to avoid confusion with the XML declaration. Otherwise, you may choose any valid XML names for your processing instructions.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are 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