Forums | MacLife
You are not logged in.
#1 2008-03-16 4:19 pm
XPATH problems
I'm farting around with XPATH and have two questions.
1. Does anyone have a function that, given a DOM node, computes the XPATH to that node from root?
2. Given the following example, how do I navigate into the table? I've tried a bunch of different queries and am getting the Not Found error every time.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>xpath</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="John Whittet">
<!-- Date: 2008-03-16 -->
<script type="text/javascript" charset="utf-8">
function goxpath (argument) {
var xpath = "/html/body/table[1]/tr[2]/td[1]";
var search_results = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
var result = search_results.iterateNext();
if (result == null) alert('Not Found');
else alert(result.tagName);
}
</script>
</head>
<body>
<input type="button" name="some_name" value="Click!" onclick="goxpath()" id="some_name">
<table border="1" cellspacing="5" cellpadding="5">
<tr><th>Header</th></tr>
<tr><td>Data 1</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
<tr><th>Header</th></tr>
<tr><td>Data</td></tr>
</table>
</body>
</html>Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline
#2 2008-03-16 5:11 pm
Online
#3 2008-03-16 6:54 pm
Re: XPATH problems
Ah. smurfin' tbody. Thanks Matt.
Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline
#4 2008-03-17 2:41 pm
Re: XPATH problems
For those looking for the second part of the original post, see below:
Code:
function buildXPath (e, base_id) {
var orig = e;
if (e == null) return null;
if (e.tagName == undefined) return null;
var xpath = "";
while (e != null) {
if (e.tagName == undefined) break;
if (e.id == base_id) break;
var tag = e.tagName;
var p = e.parentNode;
var count = 1;
var c = p.firstChild;
while (c.nextSibling != e) {
if (c.tagName == e.tagName) count++;
if (c == e) break;
c = c.nextSibling;
}
tag += "["+count+"]";
xpath = tag+"/"+xpath;
e = p;
}
xpath = xpath.substr(0, xpath.length-1); // kill the trailing slash
return xpath.toLowerCase();
}Note that the resulting paths all have [n] associated with them. This is superfluous, given that, e.g. a HTML document only has one body, and as such:
Code:
/html[1]
may be shortened to:
Code:
/html
Also note that this code returns the xpath from root. And, finally, counts start at 1 in this code, as defined by W3C. I believe IE is retarded and things that the first element is the 0th.
Last edited by Basseq (2008-03-17 2:43 pm)
Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline
#5 2008-03-17 9:33 pm
- W2ttsy
- Member
- Registered: 2002-03-04
- Posts: 3290
Re: XPATH problems
Basseq wrote:
I believe IE is retarded and things that the first element is the 0th.
This is not necessarily true. The w3c is the retard in this instance as it doesnt follow the programming convention of the fence post rule where there is always a node starting at 0 and therefore you count n-1 elements to be the length of your array or loop.
Well thats my rambling for today...
W2ttsy
punk!
modding is for rich smurfs, hacking is for pov asses like me
Offline
#6 2008-03-18 4:30 pm
Re: XPATH problems
Which is worse: writing a bad standard or ignoring the standard?
Or are you arguing that IE is not retarded? Do you love IE? Is that it?
Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline


