6

特定の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

この関数でドットファイルや他の形式のグラフを作成するために使用できる汎用グラフビルダーはありますか?

また、この目的のために明示的に設計されたツールがどこかにあるという疑惑も潜んでいます。それは何でしょうか?

4

1 に答える 1

3

python-graphを使用することになりました。また、argparseを使用して、XMLドキュメントから基本的な情報を取得し、 pydotでサポートされている形式のグラフ画像を作成するコマンドラインインターフェイスを作成しました。これはxmlearnと呼ばれ、一種の便利な機能です。

usage: xmlearn [-h] [-i INFILE] [-p PATH] {graph,dump,tags} ...

optional arguments:
  -h, --help            show this help message and exit
  -i INFILE, --infile INFILE
                        The XML file to learn about. Defaults to stdin.
  -p PATH, --path PATH  An XPath to be applied to various actions.
                        Defaults to the root node.

subcommands:
  {graph,dump,tags}
    dump                Dump xml data according to a set of rules.
    tags                Show information about tags.
    graph               Build a graph from the XML tags relationships.
于 2010-07-19T19:48:59.697 に答える