构建 XML 文档结构图

发布于 2024-09-09 22:18:45 字数 696 浏览 2 评论 0原文

我想构建一个图表,显示哪些标签用作给定 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

是否有一个通用图形生成器,我可以与此函数一起使用它来构建点文件或其他一些图形格式?

我还偷偷怀疑有一个工具是专门为此目的而设计的;那可能是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-09-16 22:18:45

我最终使用了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.

I ended up using python-graph. I also ended up using argparse to build a command line interface that pulls some basic bits of info from XML documents and builds graph images in formats supported by pydot. It's called xmlearn and is sort of useful:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文