XPath ã§ããããã ãã
cho45(ãã¨ã) / lowreal.net
ã¯ãªãã®å 容ï¼
- DOM level3 XPath
- æä½é¢æ° $X
ãã¼ã
E4X ã®è©±ã¯å ¥ããªãã£ã½ãã®ã§ $X 主ä½ã§
GM ã§ãããããã¨
- ã¯ã©ã¹åæããã (getElementsByClassName)
- ããè¦ç´ ãé¸æããã
ã¨ããæ¢åã®ããã¥ã¡ã³ããä½ãããã®ãæ®ã©ã
JavaScript ã§ã® XPath
DOM Level 3 ã®ä»æ§
- ç¾å¨ã§ã¯ Gecko ã®ã¿ãµãã¼ã
- Opera ã 9 ãããµãã¼ãï¼
ãããªã:
var xpathResult = document.evaluate( expression, context, resolver XPathResult.ANY_TYPE, null );
è¦ããã®ãããã©ã
ãããªã«ãã£ã±ãå¼æ°è¦ããããªããã
MSIE ã§ãä¸å¿ XPath èªä½ã¯ä½¿ããããã ã W3C æºæ ã®ä»æ§ã§ã¯ãªãã¦ããããã
$X é¢æ°
ä¸èº«ã¯æ°ã«ããªãã(ã¨ãããã)
å®æ°ã¨ãè¦ããã®é¢åãããã®ã§ã©ããããã
ä½ãé©å½ãª JS ãªãã¸ã§ã¯ããè¿ãã
å®è£
ã¯ãµã¤ãã«æ¸ãã¨ãã¾ãã
(æ¸ãã¦ããã¾ã)
ãã¼ã
XPath ãç´ ã§ä½¿ããã¨ããã¨çµæ§é¢åããã (å®æ°ãè¦ãããã¨ã) ã®ã§ã©ãããä½ã£ãã
å®å ¨ã«ãã©ãã¯ããã¯ã¹ã¨ãã¦ä½¿ã£ã¦ãããä¸èº«ã¯æ°ã«ããªããJS ã®è©±ãã¡ã¤ã³ãããªã (è¬)
$X ã®å®è£
ã§ãXPath ã
// ã»ã¯ã·ã§ã³ã¯ã©ã¹ãª div åæ $X("//div[@class='section']"); // æåã® h1 è¦ç´ $X("//h1[0]"); // æå¾ã® p è¦ç´ $X("//p[last()]"); // address è¦ç´ 以ä¸ã§ã // mailto ã¹ãã¼ã ããå§ã¾ã a è¦ç´ $X("//address//a[starts-with(@href, 'mailto:')]");
XPath ã¯å¼1
è¦ç´ é¸æ以å¤ã§ã使ãããï¼
$X("string()", node); //=> "Foo Bar"
<h1>Foo <em>Bar</em></h1>
ããæåå "Foo Bar" ãå¾ããã¨ã
$X ã®ç¬¬äºå¼æ°ã¯ãããã¬ãã«ã§ã®ã³ã³ããã¹ããã¼ããstring() é¢æ°ã¯å¼æ°ãçç¥ããã¨ããã³ã³ããã¹ããã¼ãã®æååå¤ãè¿ãã
XPath ã¯å¼2
ã»ãã«
$X("contains(., 'Hoge')"); //=> true/false
ããã¥ã¡ã³ãå ã«æåå "Hoge" ãããã
$X("count(//self::comment())"); //=> ex. 2
ããã¥ã¡ã³ãä¸ã®ã³ã¡ã³ãã®æ°ãæ°ãã
contains() é¢æ°ã¯å¼æ°ã§æå®ããæååãã³ã³ããã¹ããã¼ãã«å«ã¾ãã¦ããå ´å true ãè¿ãã
ã³ã¡ã³ããã¼ãã常ã®è¡¨ç¤ºãã GM ãªãããç°¡åã«æ¸ããã(ããèªä½ãæå³ãããã®ãã¯ã¨ããã)
XPath ã§ã¯ã£ã´ã¼JSãããµ
ä½ã¬ãã« DOM ã ã¨ããã©ãããã®ã楽ã