Controlling Parser BehaviorCurrently, PHP's XML parser allows you to control the following: Case folding Target encoding Whitespace processing All these attributes can be controlled via the xml_set_option() function, which accepts three parameters: A handle for the parser to be modified The attribute name The attribute value (either string or Boolean) The sections that follow describe each of these parameters in greater detail with examples. Case FoldingWithin the context of an XML document, case folding simply involves replacing lowercase characters in element names with their uppercase equivalents. XML element names are case-sensitive; typically, you use case folding to impose consistency on mixed-case element names so that they can be handled in a predictable manner. This option is controlled via the XML_OPTION_CASE_FOLDING attribute and is set to true by default. In order to see how this works, take a look at Listing 2.3 to turn off case folding (element names will no longer be uppercase). Listing 2.15 Demonstration of Case Folding
Here's the output:
Target EncodingYou already know that it's possible to specify a character set for document encoding when an XML parser is created with the xml_parser_create() function. (Refer to the "Speaking Different Tongues" sidebar at the beginning of this chapter.) In geek lingo, this is referred to as source encoding. In addition, PHP also allows you to specify target encoding, which is the encoding to use when the parser passes data to a handler function. By default, this encoding is the same as the source encoding; however, you can alter it via the XML_OPTION_TARGET_ENCODING attributes, which supports any one of the following encodings: ISO-8859-1, US-ASCII, and UTF-8. The following example sets the target encoding for the parser to UTF-8:
Whitespace ProcessingYou can tell the parser to skip the whitespace it encounters by setting the XML_OPTION_SKIP_WHITE attribute to true. This attribute can come in handy if your XML document contains tabs or spaces that could interfere with your program logic. The following example turns whitespace processing off:
You can obtain the current value of any of the parser's attributes with the xml_parser_get_option() function, which returns the value of the specified attribute. For example:
|