SCDJWS Study Guide: XML Basic


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

XML Processing Instructions

Processing instructions (PIs) are an escape hatch to provide information to an application. Like comments, they are not textually part of the XML document, but the XML processor is required to pass them to an application.

PIs allow you to enter instructions into your XML which are not part of the actual document, but which are passed up to the application.


<?xml version='1.0' encoding='UTF-16' 
   standalone='yes'?>
<name nickname='Shiny John'>
<first>John</first>
<!--John lost his middle name in a fire-->
<middle/>
<?nameprocessor SELECT * FROM blah?>
<last>Doe</last>
</name>

There aren't really a lot of rules on PIs. Processing instructions have the form: <?name pidata?>. The name, called the PI target, identifies the PI to the application. Applications should process only the targets they recognize and ignore all other PIs. Any data that follows the PI target is optional, it is for the application that recognizes the target. The names used in PIs may be declared as notations in order to formally identify them. The PI target is bound by the same naming rules as elements and attributes. So, in this example, the PI target is nameprocessor,and the actual text of the PI is SELECT * FROM blah.


Note:

  • PI names beginning with "xml" are reserved for XML standardization.

  • PIs are used to embed information for proprietary applications (not XML browser or parser)

  • PIs Often provide information about how to view unparsed external entities

  • PIs are the string of text between <? and ?>, i.e.

            
    <?xml stylesheet type="textcss" href="greeting.css"?>
            
    <?acrobat document="passport.pdf"?>

PIs are pretty rare, and are often frowned upon in the XML community, especially when used frivolously. But if you have a valid reason to use them, go for it. For example, PIs can be an excellent place for putting the kind of information (such as scripting code) that gets put in comments in HTML. While you can't assume that comments will be passed on to the application, PIs always are.

Is the XML Declaration a Procesing Instruction?

At first glance, you might think that the XML declaration is a PI that starts with xml. It uses the same "<? ?>" notation, and provides instructions to the parser (but not the application). So is it a PI?

Actually, no: the XML declaration isn't a PI. But in most cases it really doesn't make any difference whether it is or not, so feel free to look at it as one if you wish. The only places where you'll get into trouble are the following:

  • Trying to get the text of the XML declaration from an XML parser. Some parsers erroneously treat the XML declaration as a PI, and will pass it on as if it were, but many will not. The truth is, in most cases your application will never need the information in the XML declaration; that information is only for the parser. One notable exception might be an application that wants to display an XML document to a user, in the way that we're using IE5 to display the documents in this book.

  • Including an XML declaration somewhere other than at the beginning of an XML document. Although you can put a PI anywhere you want, an XML declaration must come at the beginning of a file.



Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |