Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   finding the XPath of a node (http://www.velocityreviews.com/forums/t930286-finding-the-xpath-of-a-node.html)

Jeff 03-18-2007 05:38 PM

finding the XPath of a node
 
Hi all,

I'm wondering if there is a function that will return the xpath to a
specific node given its context node. Essentially, I want the reverse
of the document.evaluate functionality. I've seen examples where
people build up the xpath themselves by walking the DOM, but I thought
there has to be a better way. I've included a simple example of this,
based on code from the Solvent project at MIT. This code works
reasonably well, but can't always narrow down to a specific node when
the node doesn't have an id assigned.

Thanks!
Jeff

function getXPath(node, doc) {
var xpath = "";

var namespace = node.ownerDocument.documentElement.namespaceURI;
var prefix = namespace ? "x:" : "";

var node2 = node;
for(var i=0; node2 && node2 != doc; i++) {
var tag = node2.tagName.toLowerCase();
var id = node2.id;
var className = node2.className;

var segment = prefix + tag;

if (id && id != "") {
xpath = "//" + segment + '[@id="' + id + '"]' + xpath;
break;
}

xpath = "/" + segment + xpath;

node2 = node2.parentNode;
}

return xpath;
}


Martin Honnen 03-18-2007 06:06 PM

Re: finding the XPath of a node
 
Jeff wrote:

> I'm wondering if there is a function that will return the xpath to a
> specific node given its context node. Essentially, I want the reverse
> of the document.evaluate functionality. I've seen examples where
> people build up the xpath themselves by walking the DOM, but I thought
> there has to be a better way.


There is nothing like "the XPath" to a node, there are usually various
XPath expressions possible. If you want an XPath expression selecting a
node I am sure there isn't a W3C DOM method doing that, you need to
write your own code walking the DOM respectively using XPath to build up
a path.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jeff 03-18-2007 08:01 PM

Re: finding the XPath of a node
 
On Mar 18, 11:06 am, Martin Honnen <mahotr...@yahoo.de> wrote:
> Jeff wrote:
> > I'm wondering if there is a function that will return the xpath to a
> > specific node given its context node. Essentially, I want the reverse
> > of the document.evaluate functionality. I've seen examples where
> > people build up the xpath themselves by walking the DOM, but I thought
> > there has to be a better way.

>
> There is nothing like "the XPath" to a node, there are usually various
> XPath expressions possible. If you want an XPath expression selecting a
> node I am sure there isn't a W3C DOM method doing that, you need to
> write your own code walking the DOM respectively using XPath to build up
> a path.


And that's what the code I pasted does. It just seems like it would
be a common-enough task that there would be a more standard way of
doing it.

I don't really care if the function is built-in, although it seems
like there are some common "types of xpaths" that people would want
(using tag names only, tags [numbers], etc.).

Maybe there's a library that has this functionality?

-Jeff


-Lost 03-19-2007 02:58 AM

Re: finding the XPath of a node
 
"Jeff" <jeffrey.bigham@gmail.com> wrote in message
news:1174248107.424063.217020@l77g2000hsb.googlegr oups.com...
> On Mar 18, 11:06 am, Martin Honnen <mahotr...@yahoo.de> wrote:
>> Jeff wrote:
>> > I'm wondering if there is a function that will return the xpath to a
>> > specific node given its context node. Essentially, I want the reverse
>> > of the document.evaluate functionality. I've seen examples where
>> > people build up the xpath themselves by walking the DOM, but I thought
>> > there has to be a better way.

>>
>> There is nothing like "the XPath" to a node, there are usually various
>> XPath expressions possible. If you want an XPath expression selecting a
>> node I am sure there isn't a W3C DOM method doing that, you need to
>> write your own code walking the DOM respectively using XPath to build up
>> a path.

>
> And that's what the code I pasted does. It just seems like it would
> be a common-enough task that there would be a more standard way of
> doing it.
>
> I don't really care if the function is built-in, although it seems
> like there are some common "types of xpaths" that people would want
> (using tag names only, tags [numbers], etc.).
>
> Maybe there's a library that has this functionality?


I believe jQuery possesses this ability. Unless I have misunderstood the requirement.

http://docs.jquery.com/Selectors

-Lost




All times are GMT. The time now is 06:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57