XML technologies / XML / Perl & XML / XML basics: reading and writing / XML::XPath

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

We have seen several parsers dutifully deliver the entire document. Often, however, the whole document is not what interests us. A database query may need only one row from a table. Opening a phone book hardly means reading it all. Clearly, we need ways to extract specific information from a document. That is what XPath provides.

XPath is another W3C Recommendation: a grammar for formal expressions identifying specific parts of any document. Think of it as an addressing scheme. Although we leave the details of XPath to Working with trees: XPath, XSLT and more, for now we can say it resembles a mixture of regular expressions and Unix-style file paths. This makes it an attractive feature to build into parsers.

The module XML::XPath by Matt Sergeant implements XPath on top of XML::Parser. Give the module an XPath expression, and it returns every matching part of the document. This is an incredibly simple way to access powerful search capabilities and the resulting data.

Consider the following XML address book:

<?xml version="1.0" encoding="ISO-8859-1"?>
<kontakte>
  <adresse>
    <name>Bob Snob</name>
    <strasse>Krösusallee 123</strasse>
    <stadt>Entenhausen</stadt>
    <plz>12345</plz>
  </adresse>
  <!-- Weitere Adressen folgen hier. -->
</kontakte>

The task is to extract all postal codes and output them as a list. The example below shows how to express this in XPath.

Example: Outputting postal codes

use XML::XPath;

my $file = 'kontakte.xml';
my $xp = XML::XPath->new(filename=>$file);

# Eine Knotenmenge von XML::XPath ist ein Objekt, das das Ergebnis
# der Anwendung eines XPath-Ausdrucks auf ein XML-Dokument enthält.
# Wir formulieren den Ausdruck und schauen, was dabei herauskommt.
my $nodeset = $xp->find('//plz');

my @zipcodes; # Ausgabe der Ergebnisse
if (my @nodelist = $nodeset->get_nodelist) {
   # Postleitzahlen wurden gefunden! Jeder Knoten ist ein Objekt der Klasse
   # XML::XPath::Node::Element. Diese Klasse besitzt eine Methode 'string_value',
   # mit der wir den darin enthaltenen Text extrahieren und ausgeben können.
   # Das tun wir für alle gefundenen Knoten.
   @zipcodes = map($_->string_value, @nodelist);
   
   # Wir sortieren die Postleitzahlen.
   @zipcodes = sort(@zipcodes);
   
   local $" = "\n";
   print "Die folgenden Postleitzahlen wurden gefunden:\n@zipcodes\n";
} else {
   print "Die Datei $file enthält keine 'plz'-Elemente!\n";
}   

Running the program on a file containing three addresses produces output like this:

Die folgenden Postleitzahlen wurden gefunden:
70565
72800
72555

Incidentally, this module is another example of tree-based XML processing. The parser loads the document into a specialized object tree, which the user queries with XPath expressions to extract particular parts. This is just one example of advanced tree-based techniques. We will see more in Working with trees: XPath, XSLT and more.

The element objects in the module XML::LibXML have a method findnodes( ), which works much like the method find( ) of XML::XPath. The calling element serves as the current context node, and the result is a list of objects matching the query. We will discuss this functionality in Programmer strategies.

  

  

 

 

 

back to top