XML technologies / XML / Perl & XML / Perl and XML / XML pitfalls

(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)

Keep these topics in mind as you read on: they cause many problems that everyone eventually encounters with XML.

Well-formedness

XML has built-in quality control: documents must satisfy minimum syntax rules. Most parsers stop reading after a single violation, so ensure your input is good enough.

Character sets

In the 21st century, unfortunately, character sets matter. Knowing only the former ruler ASCII no longer suffices. The new king is Unicode, an extensive superset of major world character sets. XML defaults to Unicode, preferably Perl’s favored variant, UTF-8. Usually this needs no attention, but remember it as a possible source of problems. [Note: in German-speaking regions, encoding is almost always relevant because UTF-8 and ISO-8859-1, common there, are not fully compatible. ISO-8859-1 is indeed an important set and a UTF-8 subset, but adopt the magic opening line <?xml version="1.0" encoding="ISO-8859-1"?> to save trouble. Translator’s note.]

Namespaces

Not everyone uses or even knows namespaces. Their usefulness emerges gradually: they distinguish markup from different specifications and let you mix document types. Equations in HTML? Markup as data in XSLT? Easily and safely, thanks to namespaces. Old modules often lack support, newer ones do not. Remember that.

Declarations

A declaration is outside the document proper but may affect distant parts, making it awkward and easily overlooked. Many documents use a DTD with declarations of entities or attributes. Defaults and similar details are easy to miss.

Entities

Entities and references seem simple: reusable content entered elsewhere, perhaps to avoid typing it ten times. An entity may live in another file or represent keyboard-inaccessible characters, such as Greek letters. Practice can be nightmarish: references usually should expand, but not always; parsers either insist on declarations or ignore them. Entities may nest arbitrarily deeply. These tricky little beasts will eventually fool you unless you watch closely.

Whitespace

XML distinguishes markup—tags, PIs and so on—from text data. Whitespace can surprise: processors should preserve it, including readability tabs and line endings. Some parsers optionally remove it in certain circumstances, but their rules are neither obvious nor consistent.

Overall, Perl and XML complement each other well. Pitfalls exist (Note: mostly XML issues—a Java or C++ programmer fares no better), but module authors have made Perl/XML problems manageable.

  

  

 

 

 

back to top