Processing Instructions

Because we have been talking so much about including data from external sources, and dealing with new types of information, such as images, Processing Instructions are the natural next step.

An XML parser actually does very little with the XML code in your documents. In fact, it's not required to do much by design; one of the goals of the authors of the specification was to make it easy for programmers to create XML parsers.

The problem with limiting what functionality the XML parser (or processor) must perform is that it limits your ability to include different types of data into your XML documents. With Notations and Entities, we've seen how you can include different types of information in your documents, such as images.

Processing Instructions (PIs) enable you to pass additional information on to the applications using your XML, beyond the XML Parser. In spite of what the name might imply, Processing Instructions are not used by the XML parser to process image (or other types of) data. Instead, Processing Instructions are used to tell the XML parser what external applications are to be utilized to deal with data of a certain type, or specific instructions for the processor related to the document at hand.

What happens is that when the XML parser is reading your XML document, and it encounters a PI, it simply hands that information off to the application the parser is part of. The parser is under no obligation to do anything else with it. If the application doesn't know what to do with the information, that's not the parser's problem. That's why if you are going to use PIs, you should make sure that you are using them correctly according to your specific application.

Processing Instructions are not a part of the document's character data. That's why the parser doesn't care about them. However, XML processors are required to pass the PI along to the application, according to the XML 1.0 Recommendation. PIs all take the following form:

<?TARGET instruction ?> 

The target is analogous to the name of the Processing Instruction, and is used to identify what application the instruction belongs to. Usually, you will want to use a notation for the target, so that there is some mechanism for associating a specific application to the PI. The only thing you can't use as a target is any form of the letters xml, as these are reserved for use by the W3C. In fact, you've already used a processing instruction with many of the examples we've used in this book:

<?xml version="1.0" ?> 

The XML declaration is in the form of a Processing Instruction. The target indicates that the instruction applies to xml, and the information provided is the version of XML we are using. That's an example of how XML uses the reserved keyword xml for its own uses.

Another example of a processing instruction might be for processing GIFs. If we have a notation for GIFs defined that specifies a GIF as a notation type, we could pass specific parameters to the application reading the file for GIFs, such as the following:

<?GIF scale="50%"> 

indicating how images are to be scaled. Of course, our PI would need to reflect instructions our application could understand. And that varies from application to application, so we can't give you any specific examples here.

Processing Instructions can come in very handy as a way of defining how XML is processed, and they are one of the mechanisms that can be used to take XML from the world of theoretical markup into practical markup. They are one of the ways in which XML is extensible, but in practice, they are used rarely. So, if you start working with XML and it's a long time before you encounter one, don't worry about it. Just keep PIs and their syntax in mind, so that if the need to use one does arise, you will remember why and how.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.161.188