xml - 如何使用XQuery创建DOT图?

标签 xml xquery dot

我需要基于以下 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 元素之间的依赖关系,这些元素使用 idxref 属性,具体取决于元素类型。

我想要的结果在下面的 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-root 元素以获取 layout-chunklayout-struct 元素及其的最佳方法是什么?可能的子级,并返回要在 DOT 图中使用的 idxref 属性?

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

最佳答案

以下查询可能会有所帮助(使用 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),
'}')

关于xml - 如何使用XQuery创建DOT图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12212792/

相关文章:

java - OpenJDK 8 (1.8.0_232) 和某些 HTTPS 连接是否存在导致 HTTP 错误 403 的已知问题?

xml - SQL 查询 varchar(max) 列中的 xml 属性

ubuntu - Graphviz 安装 Ubuntu 14.04

postgresql - 如何在同一数据库中创建包含两个模式的点文件?(PostgreSQL)

Graphviz – 节点之间的水平边

html - XSL 样式表差异

xml - 通过 cURL 在 Google 电子表格中添加一行 - 必填字段错误

android - Titanium Studio - 合金|标签的字体大小

xml - 在 XSLT 1.0 中使用带有分隔符的 concat()

sql - 如何在SQL中从xml节点提取数据