Page 331 - Open Soource Technologies 304.indd
P. 331
Unit 13: Extensible Markup Language
when it encounters a processing instruction. Set the handler with the xml_set_processing_ Notes
instruction_handler( ) function:
xml_set_processing_instruction(parser, handler);
A processing instruction looks like:
<?target instructions ?>
The processing instruction handler takes in a reference to the XML parser that triggered the
handler, the name of the target (for example, “php”), and the processing instructions:
my_processing_instruction_handler(parser, target, instructions);
What you do with a processing instruction is up to you. One trick is to embed PHP code in an
XML document and, as you parse that document, execute the PHP code with the eval( ) function.
Example does just that. Of course, you have to trust the documents you are processing if you
eval( ) code in them. eval( ) will run any code given to it even code that destroys files or mails
passwords to a hacker.
Processing instruction handler.
function processing_instruction($inParser, $inTarget, $inCode) { if ($inTarget === ‘php’) {
eval($inCode); } }
Entity Handlers
Entities in XML are placeholders. XML provides five standard entities (&, >, <, ",
and '), but XML documents can define their own entities. Most entity definitions do not
trigger events, and the XML parser expands most entities in documents before calling the other
handlers.
Two types of entities, external and unparsed, have special support in PHP’s XML library. An
external entity is one whose replacement text is identified by a filename or URL rather than
explicitly given in the XML file. You can define a handler to be called for occurrences of external
entities in character data, but it is up to you to parse the contents of the file or URL yourself if
that’s what you want.
An unparsed entity must be accompanied by a notation declaration, and while
you can define handlers for declarations of unparsed entities and notations.
External Entities
External entity references allow XML documents to include other XML documents. Typically,
an external entity reference handler opens the referenced file, parses the file, and includes the
results in the current document. Set the handler with xml_set_external_entity_ref_handler( ),
which takes in a reference to the XML parser and the name of the handler function:
xml_set_external_entity_ref_handler(parser, handler);
The external entity reference handler takes five parameters: the parser triggering the handler, the
entity’s name, the base URI for resolving the identifier of the entity (which is currently always
empty), the system identifier (such as the filename), and the public identifier for the entity, as
defined in the entity’s declaration:
$ok = my_ext_entity_handler(parser, entity, base, system, public);
If your external entity reference handler returns a false value (which it will if it returns no
value), XML parsing stops with an XML_ERROR_EXTERNAL_ENTITY_HANDLING error. If
it returns true, parsing continues.
LOVELY PROFESSIONAL UNIVERSITY 325