473,722 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.evalua te 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.ownerDocum ent.documentEle ment.namespaceU RI;
var prefix = namespace ? "x:" : "";

var node2 = node;
for(var i=0; node2 && node2 != doc; i++) {
var tag = node2.tagName.t oLowerCase();
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.parentNod e;
}

return xpath;
}

Mar 18 '07 #1
3 2260
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.evalua te 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...@yaho o.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.evalua te 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.goo glegroups.com.. .
On Mar 18, 11:06 am, Martin Honnen <mahotr...@yaho o.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.evalua te 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
2896
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> </REGION> <REGION Name="NorthAmerica" WingDing="Yellow"> <COUNTRY Name="Canada"> <TOWN Name="Quebec" WingDing="Brown" />
3
3487
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
1698
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
3598
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 ... and so on. so far so good But what happens to the xml declaration (<?xml version="1.0" ......?>).
6
1808
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 that was created and appendChild nodes to it. I can't just use document.body.appendChild as the last node is likely to be a grandchild or great-grandchild of body. I know that I could document.write an element with an id and then getElementById but...
4
5841
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
1191
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
998
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 correct. Where might I be going wrong? Any help would be appreciated! XML:
9
2155
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 should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
7
2147
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 str1, str2 As String str1 = CStr(arlsFilePath.Item(x))
0
8863
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.