I'd like to build a graph showing which tags are used as children of which other tags in a given XML document.
I've written this function to get the unique set of child tags for a given tag in an lxml.etree tree:
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
Is there a general-purpose graph builder that I could use with this function to build a dotfile or a graph in some other format?
I'm also getting the sneaking suspicion that there is a tool somewhere explicitly designed for this purpose; what might that be?