XML, the Perl Way

Processing XML with Perl Michel Rodriguez

Introduction to XML::PYX

Example: word count

Examples: PYX one-liners

Print all the elements used in an XML document, with the number of occurences.

pyx wine.xml | perl -n -e '$nb{$1}++ if( m/^\((.*)\n/); \
                         END { map { print "$_ used $nb{$_} time(s)\n";} sort keys %nb;}'

Warn in case of duplicate ID

pyx file.xml | perl -n -e '($id)=( m/^Aid (.*)\n/) or next; \
print "duplicate id: $id\n" if($id{$id});  $id{$id}=1;'

Change a tag name (class to color)

pyx wine.xml | perl -p -e 's/^([()])class$/$1color/' | pyxw


Introduction to XML::PYX

Example: word count