Eric Armstrong skrev:
> 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.

// always uses the document root as the context regardless of the 
context node provided. This is according to the XPath spec.

> 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".

Well, yes, when you are using // in the beginning of your path expression.

> 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?
> 

To get all descendants of the current context node using a shorthand you do

.//td

The . in the beginning makes sure the expression will use the current 
node as the context. There is also the following to xpath axes you can use

descendant-or-self::td (includes the context node itself)
and
descendant::td (equivalent with .//td)

/Marcus