Skip to Main Content
Perl and XML
book

Perl and XML

by Erik T. Ray, Jason McIntosh
April 2002
Intermediate to advanced content levelIntermediate to advanced
218 pages
6h 12m
English
O'Reilly Media, Inc.
Content preview from Perl and XML

Chapter 8. Beyond Trees: XPath, XSLT, and More

In the last chapter, we introduced the concepts behind handling XML documents as memory trees. Our use of them was kind of primitive, limited to building, traversing, and modifying pieces of trees. This is okay for small, uncomplicated documents and tasks, but serious XML processing requires beefier tools. In this chapter, we examine ways to make tree processing easier, faster, and more efficient.

Tree Climbers

The first in our lineup of power tools is the tree climber. As the name suggests, it climbs a tree for you, finding the nodes in the order you want them, making your code simpler and more focused on per-node processing. Using a tree climber is like having a trained monkey climb up a tree to get you coconuts so you don’t have to scrape your own skin on the bark to get them; all you have to do is drill a hole in the shell and pop in a straw.

The simplest kind of tree climber is an iterator (sometimes called a walker ). It can move forward or backward in a tree, doling out node references as you tell it to move. The notion of moving forward in a tree involves matching the order of nodes as they would appear in the text representation of the document. The exact algorithm for iterating forward is this:

  • If there’s no current node, start at the root node.

  • If the current node has children, move to the first child.

  • Otherwise, if the current node has a following sibling, move to it.

  • If none of these options work, go back up the list of ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl & LWP

Perl & LWP

Sean M. Burke
XML and Perl

XML and Perl

Mark Riehl, Ilya Sterin
Advanced Perl Programming

Advanced Perl Programming

Sriram Srinivasan
Programming Web Services with Perl

Programming Web Services with Perl

Randy J. Ray, Pavel Kulchenko

Publisher Resources

ISBN: 059600205XSupplemental ContentErrata Page