473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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;
}

Mar 18 '07 #1
3 2251
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/
Mar 18 '07 #2
On Mar 18, 11:06 am, Martin Honnen <mahotr...@yahoo.dewrote:
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

Mar 18 '07 #3
"Jeff" <je************@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Mar 18, 11:06 am, Martin Honnen <mahotr...@yahoo.dewrote:
>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
Mar 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
3
by: ricky | last post by:
I want to return with only one xpath expression the first following node relative to a node with a XPTO attribute. Example: <node> xxx </node> <node XPTO="true"> yyy
1
by: Tjerk Wolterink | last post by:
Hello all, I've written an XML file that looks a bit like this: <a> <space:b> content 1 </space:b> <b> content 2
4
by: Helmut Dirtinger | last post by:
Hi Compontents of an xml file are mapped to the different node types of the xpath data model. An element is mapped to an element node, an attribute node represents an attribute and its value...
6
by: JonQuark | last post by:
Hi, I suspect this is a common question but I've read the FAQ and searched the archives and couldn't find it. In a script embedded in the body of the page I want to quickly find the last node...
4
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
2
by: John A Grandy | last post by:
has anyone seen a situation similar to the following ? <level1 > <level2 > <level3 anAttribute="value3" /> </level2> </level1> Dim filePath As String Dim node As XMLNode
2
by: Mark Fox | last post by:
Hello, I have some XML loaded into an XmlDocument and attempting to figure out how to access certain nodes. I assume I should use XPath, but I am having trouble getting the XPath syntax...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
7
by: dotnetnoob | last post by:
i keep getting Object references not set to an instance of an object from this code: Private Sub EqBinding() Dim x As Integer x = 0 Do If CStr(arlsType.Item(x)) = "Bacnet Point" Then Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.