A processing instruction is a line of text placed at the beginning of an XML file that guides the parser in processing the data. Although a processing instruction is definitely part of the XML file, it isn't part of the XML data. A name (often referred to as the target) and content comprise a processing instruction. Question marks (?) bracket the name and content within the usual angle brackets. The following example is a typical processing instruction:
The name of the instruction is xml; the informational content of the instruction is version="1.0". You use a sequence of name/value pairs to specify the content. In the example above, you have only one name/value pair in which version is the name and 1.0 is the value. The value is always enclosed in quotation marks (").
Below is another familiar example of a processing instruction:
<?xml-stylesheet type="text/xsl" href="Foo.xsl"?>
This processing instruction associates an XML file with a particular stylesheet. In this case, the name of the processing instruction is xml-stylesheet. The content has two name/value pairs: type and href.
At this point, it should be clear that processing instructions provide general information about the file to the tools that manipulate the XML code. Such information affects the way in which the various tools (e.g., the parser) accomplish their tasks. For example, the xml-stylesheet instruction indicates that the parser should use the Extensible Style Language (XSL) stylesheet to render the XML document.
You can insert processing instructions statically when you write the file with a text editor or dynamically (i.e., on the fly) using the createProcessingInstruction method. Here's how to add a processing instruction on the fly:
Set xml = CreateObject("Microsoft.XMLDOM") xml.async = False xml.load "foo.xml" Set pi = xml.createProcessingInstruction("author", _ "name=""DinoE"" dept=""Sales""") xml.insertBefore pi, xml.childNodes.item(0)
The createProcessingInstruction method takes two string arguments: The first denotes the name of the instruction; the second is a blank-separated string with all the content pairs. So the code above adds to the beginning of foo.xml an instruction named author that contains the name and department of the page's author. The line of text that is actually added to the XML code looks like this:
<?author name="DinoE" dept="Sales"?>
The createProcessingInstruction method does two things: It formats the information by adding question marks and angle brackets, and it creates a node object of type NODE_PROCESSING_INSTRUCTION. This node's nodeName property is set to the instruction name. The nodeValue property contains the instruction content. When createProcessingInstruction returns, the parser hasn't yet added the new node to the document tree. But the new node does exist in the context of the document, and its ownerDocument property correctly points to the host document. If you execute the following instruction after you call createProcessingInstruction, the system returns the original source code of the parent document:
Because the system returns the original source code of the parent document, you know two things: the node has been created and linked to the existing tree but the source code hasn't been modified yet to include the processing instruction. To force source-code modification, you need to explicitly call insertBefore and specify the node as well as the point of insertion. The simple example we've used here inserts the processing instruction at the top of the page.