特定のXMLドキュメントでどのタグが他のどのタグの子として使用されているかを示すグラフを作成したいと思います。
この関数は、lxml.etreeツリー内の特定のタグの子タグの一意のセットを取得するために作成しました。
def iter_unique_child_tags(root, tag):
"""Iterates through unique child tags for all instances of tag.
Iteration starts at `root`.
"""
found_child_tags = set()
instances = root.iterdescendants(tag)
from itertools import chain
child_nodes = chain.from_iterable(i.getchildren() for i in instances)
child_tags = (n.tag for n in child_nodes)
for t in child_tags:
if t not in found_child_tags:
found_child_tags.add(t)
yield t
この関数でドットファイルや他の形式のグラフを作成するために使用できる汎用グラフビルダーはありますか?
また、この目的のために明示的に設計されたツールがどこかにあるという疑惑も潜んでいます。それは何でしょうか?