finding the XPath of a node

Discussion in 'Javascript' started by Jeff, Mar 18, 2007.

  1. Jeff

    Jeff Guest

    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;
    }
    Jeff, Mar 18, 2007
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Mar 18, 2007
    #2
    1. Advertising

  3. Jeff

    Jeff Guest

    On Mar 18, 11:06 am, Martin Honnen <> 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
    Jeff, Mar 18, 2007
    #3
  4. Jeff

    -Lost Guest

    "Jeff" <> wrote in message
    news:...
    > On Mar 18, 11:06 am, Martin Honnen <> 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
    -Lost, Mar 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,399
  2. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,953
    jan V
    Jul 29, 2005
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,364
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. Scott W Gifford
    Replies:
    7
    Views:
    2,485
    Scott W Gifford
    Aug 15, 2005
  5. Tjerk Wolterink
    Replies:
    2
    Views:
    1,399
    Dimitre Novatchev
    Aug 24, 2006
Loading...

Share This Page