Eric Armstrong wrote:
> This code looks for a table that matches
> specific criteria:
> 
>     XPath.each(@doc, '//table') do |tbl|
>       XPath.each(tbl, '//td') do |td|
>         # look for matching data
> 
> According to the XPath doc, the first argument
> is the "context" element.
> 
> So what /should/ happen is that search
> for td elements occurs in the subtree
> rooted at the tbl element, using the
> //td path --which I take to mean "anywhere
> within the context".
> 
> But what actually happens is that the
> search for td elements occurs in the
> entire document, so the code above returns
> the first table, regardless of where the
> matching data is found.

That sounds like a bug.

> The workaround is to dispense with the
> outer loop. Once matching data is found
> and keep visiting parents until the
> table ancestor is found. (That patch
> simplifies the code, actually.)

IMHO the proper solution is to craft a single XPath expression that will 
cover your requirement, i.e. any "td" somewhere below a "table".

> But if this implementation isn't a bug, it
> means that the definition of "context" is
> "the entire tree in which the specified
> node is found".
> 
> In that case, the XPath expression that
> asks for "all <td> elements under the
> current <table> node" must be something
> other than what I coded...
> 
> What might that path be, I wonder?

I'm not too involveld with XPath but did you try something like this?

//table/*/td
//table//td

IMHO having a single XPath expression is the preferred way to go.

Also, I usually use method doc.elements.each 'xpath here' do ... instead 
of the specific XPath expression you used.

Kind regards

	robert