XML technologies / XML / Perl & XML / Event streams / XML::PYX
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
Standardized APIs took some time to establish themselves in the Perl world, for several reasons. CPAN, a veritable bargain bin of freely copyable modules, grows organically. There is no central authority approving new software. Although the Perl community embraced XML early as a relative newcomer among data formats, demand for standard solutions came later.
This early era of Perl XML hacking could be called the age of individual parsers. Documentation was sparse, modules were experimental and constantly changing. There was much creativity and innovation, but just as much individualism. Surprisingly, some of these older tools were exceptionally useful: an interesting subject for historians and developers alike.
XML::PYX is one of these early parsers. Streams naturally lead to the concept of Pipeline. The output of one small, simple program becomes the input of another equally small program. A large, complex program is built by combining smaller processors. There is no reason not to apply this principle to XML. This concept inspired an innovative and elegant style of processor implementation. Essentially, XML is converted into a stream of symbols that are easy to recognize and transform, an approach that works nicely even as a command-line application.
PYX is an example of this concept. XML markup is converted to a format suitable both for a text-oriented language such as Perl and for Unix programs such as awk and grep . These programs operate line by line, so PYX puts each event on a separate line.
The following table summarizes PYX syntax.
Table: PYX notation
Symbol | Meaning |
( | Element start tag |
) | Element end tag |
- | Character data |
A | An attribute |
? | A PI |
For each event from the stream, PYX starts a new line with one of the five symbols in this table. Further data, such as the element name, follows depending on the event type. Special characters are escaped with a backslash, as in Perl.
Let us see what happens when an XML parser’s output is converted to PYX notation. Consider the following XML document:
<einkaufsliste>
<!-- Marken sind uns egal -->
<artikel>Zahnpasta</artikel>
<artikel>Kabrio</artikel>
<artikel optional="yes">Kaviar</artikel>
</einkaufsliste>In PYX it would look like this:
(einkaufsliste
-\n
(artikel
-Zahnpasta
)artikel
-\n
(artikel
-Kabrio
)artikel
-\n
(artikel
Aoptional yes
-Kaviar
)artikel
-\n
)einkaufslisteNotice that the comment disappears in the PYX translation. PYX is minimalist in several respects, omitting some aspects of markup. For example, it does not report CDATA sections, only their contents. The most significant loss, however, is any entity references, which are removed from the stream. Before considering PYX, make sure you can do without these features.
Matt Sergeant wrote a module called XML::PYX. This module reads an XML document and converts it to PYX. The program in the following example uses it to remove all element tags, leaving only text data.
Example: PYX parser
use XML::PYX;
# Initialisierung des Parsers und Ausgabe von PYX
my $parser = XML::PYX::Parser->new;
my $pyx;
if (defined ( $ARGV[0] )) {
$pyx = $parser->parsefile( $ARGV[0] );
}
# Entfernung der Tags
foreach( split( / /, $pyx )) {
print $' if( /^-/ );
}PYX is an interesting alternative to SAX and DOM, when you need a quick solution to a small problem. It handles tasks such as counting elements, separating text from markup, and recognizing simple events particularly well, especially if you are familiar with the Unix tools mentioned earlier, such as wc, grep or awk. However, PYX notation has fairly limited capabilities, which becomes apparent over time. This makes the module unsuitable for more complex tasks.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top