The 2001 Perl Advent Calendar
[about] | [archives] | [contact] | [home]

On the 5th day of Advent my True Language brought to me..
XML::XPath

What can I say about XML::XPath? As far as I'm concerned it is the most useful module for processing XML in my current arsenal. XPath is often described as "regular expressions for XML," which is a good, if not slightly inaccurate place to start.

XML::XPath allows you to drill down from the root node, or relative to any other node you may have access to depending on some fuzzy rules that you have defined. It's just like any other declarative language where you simply state what you want and let the engine churn away with it's own matching strategy, backtracking till you get what you want. Of course, you get to ignore how the computer does all of this; Like all my favourite modules this one just works.

One of the great things about using XPath in Perl is that fact that you're not forced into using the declarative syntax if you don't want to. At any point Matt has programmed it so you can call any of the object orientated methods on the the node objects directly to move around the tree that way - or indeed mix and match the two picking whichever is best suited to the job. This is Perl, and yet again There's More Than One Way To Do It.

  • XML::XPath::Node for descriptions of the nodes
  • Template::Plugin::XML::XPath for the Template Toolkit Plugin
  • XML::XPath::Simple for the simple interface
  • XML::LibXML for a similar C library