【问题标题】:How to use XQuery to create a DOT graph?如何使用 XQuery 创建 DOT 图?
【发布时间】:2012-08-31 20:06:15
【问题描述】:

我需要在以下XML的基础上创建一个DOT图:

<layout-structure>
    <layout-root id="layout-root">
        <layout-chunk id="header-text">
            <layout-leaf xref="lay-1.01"/>
            <layout-leaf xref="lay-1.02"/>
        </layout-chunk>
        <layout-leaf xref="lay-1.03"/>
    </layout-root>
</layout-structure>

我想使用 DOT 来可视化不同 layout-chunklayout-leaf 元素之间的依赖关系,这些元素使用任一 id标识> 或 xref 属性,具体取决于元素类型。

我想要的结果在下面用 DOT 给出:

graph "layout-root" {
"layout-root" -- "header-text";
"header-text" -- "lay-1.01";
"header-text" -- "lay-1.02";
"layout-root" -- "lay-1.03";
}

这将导致这个可视化图表:

使用 XQuery 为 layout-chunklayout-structure 元素及其元素解析 layout-root 元素的最佳方法是什么可能的孩子,并返回要在 DOT 图中使用的 idxref 属性?

我是 XQuery 的新手,尝试过各种方法;我想我需要连接每个元素中的 idxref 值,以便为 DOT 生成所需的标记。

【问题讨论】:

    标签: xml xquery dot


    【解决方案1】:

    以下查询可能会有所帮助(使用 BaseX 和 Saxon 进行测试):

    declare variable $nl := '&#10;';
    
    declare function local:ref($root) {
      string-join((
        for $c in $root/layout-chunk
        return (
          concat('  "', $root/@id, '" -- "', $c/@id, '";', $nl),
          local:ref($c)
        ),
        local:leaf($root)), "")
    };
    
    declare function local:leaf($root) {
      for $c in $root/layout-leaf
      return concat('  "', $root/@id, '" -- "', $c/@xref, '";', $nl)
    };
    
    (: Alternative: let $doc := doc("doc.xml") :)
    let $doc := document {
      <layout-structure>
          <layout-root id="layout-root">
              <layout-chunk id="header-text">
                  <layout-leaf xref="lay-1.01"/>
                  <layout-leaf xref="lay-1.02"/>
              </layout-chunk>
              <layout-leaf xref="lay-1.03"/>
          </layout-root>
      </layout-structure> }
    let $root := $doc/layout-structure/*
    return concat(
      'graph "', $root/name(), '" { ' , $nl,
      local:ref($root),
    '}')
    

    【讨论】:

    • 非常感谢,@christian-grun!我得到了大部分代码,但是您能否在第二个函数声明中引导我完成这部分:return ( concat(' "', $root/@id, '" -- "', $c/@id, '";', $nl), local:ref($c) ), local:leaf($root)), "") }; concat 函数之后的代码到底做了什么?
    • 不知道从哪里开始......你到底需要了解什么?
    • 没关系,在阅读了一些关于函数定义的内容后才明白——这对我来说是新事物!不过,非常感谢!
    【解决方案2】:

    Dot 有一种 XML 语法,称为 DotML。我发现生成 DotML 比直接生成 Dot 更容易。详情在这里:

    http://martin-loetzsch.de/DOTML/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多