Created
March 29, 2011 20:29
-
-
Save t-f-m/893182 to your computer and use it in GitHub Desktop.
XPathの動作確認
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script> | |
//utilities via AutoPagerize | |
function getElementsByXPath(xpath, node) { | |
var nodesSnapshot = getXPathResult(xpath, node, | |
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) | |
var data = [] | |
for (var i = 0; i < nodesSnapshot.snapshotLength; i++) { | |
data.push(nodesSnapshot.snapshotItem(i)) | |
} | |
return data | |
} | |
function getXPathResult(xpath, node, resultType) { | |
var node = node || document | |
var doc = node.ownerDocument || node | |
var resolver = doc.createNSResolver(node.documentElement || node) | |
// Use |node.lookupNamespaceURI('')| for Opera 9.5 | |
var defaultNS = node.lookupNamespaceURI(null) | |
if (defaultNS) { | |
const defaultPrefix = '__default__' | |
xpath = addDefaultPrefix(xpath, defaultPrefix) | |
var defaultResolver = resolver | |
resolver = function (prefix) { | |
return (prefix == defaultPrefix) | |
? defaultNS : defaultResolver.lookupNamespaceURI(prefix) | |
} | |
} | |
return doc.evaluate(xpath, node, resolver, resultType, null) | |
} | |
function check(){ | |
one=getElementsByXPath('id("one")/li[self::li/a/@href=preceding-sibling::li/a/@href]/a') | |
for(a=one[0],i=0;i<one.length;a=one[i],++i) | |
alert("one: "+a.title+" "+a.href) | |
two=getElementsByXPath('id("two")/li[self::li/a/@href=preceding-sibling::li/a/@href]/a') | |
for(a=two[0],i=0;i<two.length;a=two[i],++i) | |
alert("two: "+a.title+" "+a.href) | |
three=getElementsByXPath('id("three")/li[self::li/a/@href=preceding-sibling::li/a/@href]/a') | |
for(a=three[0],i=0;i<three.length;a=three[i],++i) | |
alert("three: "+a.title+" "+a.href) | |
one=getElementsByXPath('id("onedash")/li[substring-before(self::li/a/@href,"p")=substring-before(preceding-sibling::li/a/@href,"n")]/a') | |
for(a=one[0],i=0;i<one.length;a=one[i],++i) | |
alert("one': "+a.title+" "+a.href) | |
two=getElementsByXPath('id("twodash")/li[substring-before(self::li/a/@href,"p")=substring-before(preceding-sibling::li/a/@href,"n")]/a') | |
for(a=two[0],i=0;i<two.length;a=two[i],++i) | |
alert("two': "+a.title+" "+a.href) | |
three=getElementsByXPath('id("threedash")/li[substring-before(self::li/a/@href,"p")=substring-before(preceding-sibling::li/a/@href,"n")]/a') | |
for(a=three[0],i=0;i<three.length;a=three[i],++i) | |
alert("three': "+a.title+" "+a.href) | |
} | |
window.onload=check | |
</script> | |
</head> | |
<body> | |
<ul id="one"> | |
<li><a href="0000" title="prev">prev</a></li> | |
<li><a href="0001" title="main">main</a></li> | |
<li><a href="0001" title="next">next</a></li> | |
</ul> | |
<ul id="two"> | |
<li><a href="0001" title="prev">prev</a></li> | |
<li><a href="0001" title="main">main</a></li> | |
<li><a href="0001" title="next">next</a></li> | |
</ul> | |
<ul id="three"> | |
<li><a href="0001" title="prev">prev</a></li> | |
<li><a href="0001" title="main">main</a></li> | |
<li><a href="0002" title="next">next</a></li> | |
</ul> | |
<ul id="onedash"> | |
<li><a href="0000p" title="prev">prev</a></li> | |
<li><a href="0001n" title="main">main</a></li> | |
<li><a href="0001p" title="next">next</a></li> | |
</ul> | |
<ul id="twodash"> | |
<li><a href="0001p" title="prev">prev</a></li> | |
<li><a href="0001n" title="main">main</a></li> | |
<li><a href="0001p" title="next">next</a></li> | |
</ul> | |
<ul id="threedash"> | |
<li><a href="0001p" title="prev">prev</a></li> | |
<li><a href="0001n" title="main">main</a></li> | |
<li><a href="0002p" title="next">next</a></li> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment