4
我想要構建一個圖表,顯示哪些標記被用作給定XML文檔中其他標記的子元素。構建一個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
有,我可以用這個使用通用的圖形生成器函數來以某種其他格式構建點文件或圖形?
我也越來越懷疑有一個工具明確地爲此目的而設計;那可能是什麼?