どう書くXPath:ここからここまで

XPathの不便なところ - ぶろぐ。@はてなより、http://labs.cybozu.co.jp/blog/akky/archives/2009/02/interviewd-by-junior-high.htmlの本文を取るXPathを考えてみた。

とりあえず、2パターン。*1

id("center")/div/*[preceding-sibling::*[following-sibling::h2] and self::*[not(preceding-sibling::p[@class="posted" and a])]]
id("center")/div/*[self::h2[following-sibling::h3] or (preceding-sibling::h2 and following-sibling::p[@class="posted" and a]) or self::p[@class="posted" and a] ]

うーん、確かにいまひとつ。1つ目のほうが上手い感じだけど、理解し難い。2つ目は単に3つ繋げただけなのでベタだけどわかり易いかな。
ここよりも後でここより前を指定するXPath - FFFF - 0x

*1:結果に影響しないので、node()ではなく*を使っているけど、テキストノードも取得する必要がある場合はnode()を使う必要がある