Re: [xsl] XPath needed for getting the nearest attribute on the ancestor axis till a Node with a particular name() is found

Subject: Re: [xsl] XPath needed for getting the nearest attribute on the ancestor axis till a Node with a particular name() is found
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 22 Aug 2001 15:08:27 +0100
Hi Sanjay,

> I needed to get the nearest attribute value (with name() 'name' or 'type'
> or 'category') on the ancestor axis, starting from a particular node. I am
> able to do that, thanks to this list too for that, using the following:
>             xpathStr = "./ancestor::*[@name or @type or
> @category][1]/@*[name() = 'name' or name() = 'type' or name()
> = 'category']";
>             XObject obj = XPathAPI.eval(currentNode,xpathStr);
> It works fine. Now I want to limit the ancestor axis traversal till I find
> a node with name() = 'someName', otherwise the search would continue till
> root node.

All the ancestors up until the 'someName' element will have a
'someName' element as an ancestor, so you could use:

  ancestor::*[ancestor-or-self::someName][@name or @type or @category][1]
    /@*[name() = 'name' or name() = 'type' or name() = 'category']

This will stop at the last (highest in the tree) 'someName' element;
if there isn't a 'someName' element in the ancestors of the current
node, then it won't return any nodes.

If you want to stop at the first (closest to the current node)
'someName' element, then you could use:

  ancestor::*[not(descendent::someName)][@name or @type or @category][1]
    /@*[name() = 'name' or name() = 'type' or name() = 'category']

Both of these paths will, in most cases, be a lot less efficient than
using the original path because they will cause more node visits -
with each visit to an ancestor of the current node, you will visit all
its ancestors (or all its descendents) as well. I would only use one
of these XPaths if there's a reason you want to stop before the root
node due to the logic of the application, not for efficiency reasons.
(As with all statements about efficiency, you should try it out on
your own data and with your own processor to see whether the change
has any noticeable effect.)

[BTW, there's no need to start any path with './', which just says
"from the current node" - all relative paths go from the current node
by default.]

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread