Greetings monks. Lately I've been working on a class implimenting a tree data structure. Each object of the class has a list of children which may contain any number of other objects of the same class, which may themselves have children. Each node has a name which identifies the type of node, like 'page' or 'paragraph'.

Many of the most common operations performed on these objects only want to affect a sub-set of the available children. For example, a loop that wants to process all paragraphs might look like:

foreach my $child ($obj->children()) { next unless $child->name eq 'paragraph'; # code goes here }

More complex cases involve needing to operate on a subset of children defined by two or more levels of containership. For example, to collect all the headers on all the pages:

foreach my $child ($obj->children()) { next unless $child->name eq 'page'; foreach my $grandchild ($child->children()) { next unless $grandchild->name eq 'header'; # code goes here } }

Suddenly it occurred to me that I've seen patterns like this before, in code dealing with XML. And I've seen a good solution to the problem, XPath, which I've never really gotten a good chance to use. So, today I added a method to do XPath-style matching called match(). It only supports the simplest patterns, but I plan to improve it incrementally.

For example, the first block of code can be translated to:

foreach my $para ($obj->match('paragraph')) { # code goes here }

That's no big deal, but the fun starts translating the second one:

foreach my $header ($obj->match('/page/header')) { # code goes here }

Once I wrote match(), I realized I had another tool I didn't even realize I could use. I now have a way to unqiuely identify a node in a tree. For example, to get the third paragraph on the fourth page:

   ($para) = $obj->match('/page[3]/paragraph[2]');

So not only is match() a simpler and cleaner interface to selecting nodes from the tree, it also offers an entirely novel feature: unique indentifiers for nodes inside the tree. I added an xpath() method to return this path for a given node. I've already found one area in the application where this significantly reduces code complexity and I expect to find more.

-sam


In reply to XPath matching for object trees by samtregar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.