getting XPath of a node

Discussion in 'Java' started by Josef Garvi, Apr 14, 2004.

  1. Josef Garvi

    Josef Garvi Guest

    Is it possible with the new 1.5 JDK's XPath libraries to retrieve the XPath
    of a given node?

    Something like:

    String xpathExpression = XPathFactory.createFromNode(doc, node);


    --
    Josef Garvi

    "Reversing desertification through drought tolerant trees"
    http://www.eden-foundation.org/

    new income - better environment - more food - less poverty
     
    Josef Garvi, Apr 14, 2004
    #1
    1. Advertising

  2. Josef Garvi

    Chris Smith Guest

    Josef Garvi wrote:
    > Is it possible with the new 1.5 JDK's XPath libraries to retrieve the XPath
    > of a given node?
    >
    > Something like:
    >
    > String xpathExpression = XPathFactory.createFromNode(doc, node);


    I don't see anything off-hand.

    In general, there are an infinite number of different valid XPath
    expressions that will refer to the same node. A lot of choices would
    need to be made (for example, whether to index into child nodes by some
    attribute value, or positionally -- and if positionally, should only
    elements of a given tag name be considered or should the global position
    be used, etc.) Once you've resolved all of these ambiguities, it
    shouldn't be too difficult to implement this from a DOM model.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 14, 2004
    #2
    1. Advertising

  3. Josef Garvi

    Bryce (Work) Guest

    On Wed, 14 Apr 2004 10:02:51 -0600, Chris Smith <>
    wrote:

    >Josef Garvi wrote:
    >> Is it possible with the new 1.5 JDK's XPath libraries to retrieve the XPath
    >> of a given node?
    >>
    >> Something like:
    >>
    >> String xpathExpression = XPathFactory.createFromNode(doc, node);

    >
    >I don't see anything off-hand.


    I would think the default way would be from the root, such as
    /rootNode/someParent/anotherParent/ourNode

    XML Spy can do it. I don't know much about the new XPath libraries,
    but if you wanted to write it your self, I guess you'd just start with
    your node, traverse to it's parent, etc. Each node will only have one
    parent. Keep traversing til you get to the root. Has the markings of a
    recursive function to me... Something like:

    Get Parent Node:
    if Has Parent
    Get Parent Node

    return name

    --
    now with more cowbell
     
    Bryce (Work), Apr 14, 2004
    #3
  4. Josef Garvi

    Chris Smith Guest

    Bryce (Work) wrote:
    > I would think the default way would be from the root, such as
    > /rootNode/someParent/anotherParent/ourNode
    >
    > XML Spy can do it. I don't know much about the new XPath libraries,
    > but if you wanted to write it your self, I guess you'd just start with
    > your node, traverse to it's parent, etc. Each node will only have one
    > parent. Keep traversing til you get to the root.


    That would certainly be doable if you can assume, for example, that
    there is only one child of "rootNode" called "someParent". If not, then
    the ambiguities I mentioned begin to take place.

    Incidentally, it would be just as easy to implement this iteratively as
    recursively:

    String path = "";
    for (Node n = node; n != doc; n = n.getParentNode())
    {
    String localXPath = ...;
    path = localXPath + "/" + path;
    }

    path = "/" + path;

    Since the loop is unlikely to be long-lived, I wouldn't worry about the
    string building inefficiencies there.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 14, 2004
    #4
  5. Josef Garvi

    Josef Garvi Guest

    Chris Smith wrote:
    >
    > That would certainly be doable if you can assume, for example, that
    > there is only one child of "rootNode" called "someParent". If not, then
    > the ambiguities I mentioned begin to take place.


    I noticed that XPath Explorer (http://sourceforge.net/projects/xpe/) solves
    this by positional indexing within the element type. It gives a unique
    reference to the child node as long as the order of child nodes remains
    unchanged. Modifying the document (without modifying the node in question)
    will invalidate the obtained path.

    Referencing to child nodes by some unique attribute would be better in that
    sense, but it's hard to do generically. I guess it could be done by
    analysing the schema file in some way, though... (assuming that all
    elements have some kind of unique identification).

    --
    Josef Garvi

    "Reversing desertification through drought tolerant trees"
    http://www.eden-foundation.org/

    new income - better environment - more food - less poverty
     
    Josef Garvi, Apr 15, 2004
    #5
    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,569
  2. Marvin_123456

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

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,995
    jan V
    Jul 29, 2005
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,445
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. Anna
    Replies:
    0
    Views:
    542
  5. Tjerk Wolterink
    Replies:
    2
    Views:
    1,459
    Dimitre Novatchev
    Aug 24, 2006
Loading...

Share This Page