lxml
's ( itersibling()
) を使用し、子孫ではなく兄弟で作業し、必要に応じてこれらの兄弟の子孫で作業する方がおそらく簡単です。
このようなものを試すことができます
>>> for heading in root.iter("h3"):
... print "----", heading
... for sibling in heading.itersiblings():
... if sibling.tag == 'h3':
... break
... print sibling
...
---- <Element h3 at 0x1880470>
<Element p at 0x18800b0>
<Element p at 0x1880110>
<Element a at 0x1880170>
---- <Element h3 at 0x1880050>
<Element p at 0x18801d0>
>>>
XPath を使用する場合は、 (名前空間を介して)で利用可能なEXSLT のset 拡張機能を使用できます。考え方は上記とほぼ同じです。lxml
"http://exslt.org/sets"
- すべての兄弟を選択 (
following-sibling::*
)、
- ただし、除外 (
set:difference()
) 次の<h3>
兄弟 ( following-sibling::h3
) および ( |
XPath 演算子) 後続のすべての兄弟も ( following-sibling::h3/following-sibling::*
)。
これは次のように使用できます。
>>> following_siblings_untilh3 = lxml.etree.XPath("""
... set:difference(
... following-sibling::*,
... (following-sibling::h3|following-sibling::h3/following-sibling::*))""",
... namespaces={"set": "http://exslt.org/sets"})
>>>
>>> for heading in root.iter("h3"):
... print "----", heading
... for e in following_siblings_noth3(heading): print e
...
---- <Element h3 at 0x1880470>
<Element p at 0x18800b0>
<Element p at 0x1880110>
<Element a at 0x1880170>
---- <Element h3 at 0x1880050>
<Element p at 0x18801d0>
>>>
単純化できると確信しています。(私は見つけていませんfollowing-sibling-or-self::h3
...)