我需要基于以下 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-chunk 和 layout-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-root 元素以获取 layout-chunk 和 layout-struct 元素及其的最佳方法是什么?可能的子级,并返回要在 DOT 图中使用的 id 和 xref 属性?
我是 XQuery 的新手,并且尝试过各种方法;我认为我需要连接每个元素中的 id 和 xref 值,以便生成 DOT 所需的标记。
最佳答案
以下查询可能会有所帮助(使用 BaseX 和 Saxon 进行测试):
declare variable $nl := ' ';
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/