Misuse of Processing Instructions

Clearly, processing instructions should not be used for information that is properly part of the domain of the XML application. For example, it would be inappropriate to use processing instructions to specify when and where a particular chess game was played . Markup like the following is to be eschewed.

 <?xml version="1.0"?> <?game-info date="2003-10-24" white="Jane Smith"                               black="Alice Jones"?> <game>   <move>f3</move>   <move>e5</move>   <move>g4</move>   <move>Qh4++</move> </game> 

Instead, additional elements and attributes should be added to the vocabulary to encode the additional information.

 <?xml version="1.0"?> <game>   <date>2003-10-24</date>   <white>Jane Smith</white>   <black>Alice Jones</black>   <move>f3</move>   <move>e5</move>   <move>g4</move>   <move>Qh4++</move> </game> 

For the same reason, when the whole purpose of an XML application is to display content to end users, presentation style information should be encoded in the markup. It would be inappropriate, for example, to add an xml-stylesheet processing instruction to an XHTML document. Instead, you should use the elements and attributes XHTML provides for this purpose: link , style , and so forth.

One reason developers sometimes use processing instructions where they shouldn't is to include necessary information the DTD or schema doesn't provide a place for. However, this means you can't use the basic XML tools to extract the structure from these instructions, as you can from elements. You also run the risk that other processes will, intentionally or not, strip out the processing instructions from the document. For instance, by default an XSLT transformation throws away all processing instructions that are not targeted by the stylesheet.

Ideally, what needs to be done in this case is to update the DTD or schema. However, even if that isn't possible, this shouldn't stop you from adding the extra elements you need. XML is the Extensible Markup Language. You can add pieces to it that aren't expected. Invalid documents can still be usefully processed .

Processing instructions should be used to provide clues to specific processes that operate on the document. They should be used for information that is orthogonal to the document's own content. And they should be used with great caution. Processing instructions should not be your first resort. Always try to fit the data into the element markup and content if it's at all reasonably possible. However, sometimes it isn't, and processing instructions become a useful escape hatch from the usual strictures of XML.

Effective XML. 50 Specific Ways to Improve Your XML
Effective XML: 50 Specific Ways to Improve Your XML
ISBN: 0321150406
EAN: 2147483647
Year: 2002
Pages: 144

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net