1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<html>
<head>
<title>DOM Level 3 XPath Example</title>
<script language="javascript" type="text/javascript" src="html-xpath.js"></script>
</head>
<body>
<div class="Test">
Test Node <span class="Number">1</span>
</div>
<span id="TestingGround">
<div class="SomeOtherClass">
<div class="Test" id="ContextNodeTest">
Test Node <span class="Number">2</span>
<p>Test Node <span class="Number">3</span>: Unclosed Paragraph tag
<div class="Test">
Test Node <span class="Number">4</span>: Nesting
</div>
<img src="rainbow.jpg" alt="Something I found in my backyard" width="100" height="200" alt="Test image" someOtherAttribute="someOtherValue" />
<img src="carnation.jpg" alt="Carnation" width="99" height="74">
</div>
</div>
</span>
<script language="javascript">
function showIterator(title, result)
{
var s = title;
var item;
while(item = result.iterateNext())
{
s += "<div class=\"XPathResultItem\">" + (item.nodeType == 1 ? item.innerHTML.replace(/</g, "<").replace(/>/g, ">") : item.nodeValue) + "</div>";
}
document.write(s);
}
// Evaluate 1
var result = document.evaluate("//div[@class='Test']", document, null, XPathResult.STRING_TYPE, null);
document.write("<h1>\"//div[@class='Test']\" as String</h1>" + result.getStringValue());
// Evaluate 2
document.evaluate("//img[@width < 100]/@height", document, null, XPathResult.NUMBER_TYPE, result);
document.write("<h1>\"//img[@width < 100]/@height\" as Number</h1>" + result.getNumberValue());
// Evaluate 3
showIterator
(
"<h1>\"//div[@class='Test']\" in context of document</h1>",
document.evaluate("//div[@class='Test']", document, null, XPathResult.ANY_TYPE, result)
);
// Evaluate 4
showIterator
(
"<h1>\"div[@class='Test']\" in context of a specific node (id=\"ContextNodeTest\")</h1>",
document.evaluate("div[@class='Test']", document.getElementById("ContextNodeTest"), null, XPathResult.ANY_TYPE, result)
);
// Evaluate 5
showIterator
(
"<h1>\"//div/@class\": Retrieve Attribute Values</h1>",
document.evaluate("//div/@class", document, null, 0, result)
);
// Evaluate 6
var result = document.evaluate("count(//span[@class='Number'])", document, null, XPathResult.NUMBER_TYPE, null);
document.write("<h1>\"count(//span[@class='Number'])\" as Number</h1>" + result.getNumberValue());
</script>
</body>
</html>
|