我需要在以下 XML 文件的基础上创建一个 DOT 图。
<layout>
<layout-structure>
<layout-root id="layout-root">
<layout-chunk id="header-text">
<layout-leaf xref="lay-1.01" location="row-1" area-ref="content"/>
<layout-leaf xref="lay-1.02" location="row-2" area-ref="content"/>
</layout-chunk>
<layout-leaf xref="lay-1.03" location="row-3" area-ref="content"/>
</layout-root>
<layout-root id="layout-root-two">
<layout-chunk id="header-text-two">
<layout-leaf xref="lay-1.04"/>
<layout-leaf xref="lay-1.05"/>
</layout-chunk>
</layout-root>
</layout-structure>
<realization>
<text xref="lay-1.01 lay-1.04"/>
<text xref="lay-1.02 lay-1.05"/>
<graphics xref="lay-1.03"/>
</realization>
<layout>
为此,我使用以下查询为 XML 中的每个布局根元素生成 DOT 标记:
declare variable $newline := ' ';
declare variable $dotgraphics := '[shape="box", style="filled", color="#b3c6ed"]';
declare function local:ref($root) {
string-join((
for $chunk in $root/layout-chunk
return (
concat(' "', $root/@id, '" -- "', $chunk/@id, '";', $newline),
local:ref($chunk)
),
local:leaf($root)), "")
};
declare function local:leaf($root) {
for $leaf in $root/layout-leaf
return concat(' "', $root/@id, '" -- "', $leaf/@xref, '";', $newline)
};
declare function local:gfx($doc) {
for $layout-leafs in $doc//layout-leaf
let $graphics := $doc/realization//graphics
where $graphics[contains(@xref, $layout-leafs/@xref)]
return concat('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)
};
let $doc := doc("layout-data.xml")/layout
for $root in $doc/layout-structure/*
return string-join(('graph "', $root/@id, '" { ', $newline,
local:gfx($doc),local:ref($root),'}', $newline), "")
两个layout-root元素的查询结果如下所示:
graph "layout-root" {
"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];
"layout-root" -- "header-text";
"header-text" -- "lay-1.01";
"header-text" -- "lay-1.02";
"layout-root" -- "lay-1.03";
}
graph "layout-root-two" {
"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];
"layout-root-two" -- "header-text-two";
"header-text-two" -- "lay-1.04";
"header-text-two" -- "lay-1.05";
}
如您所见,如果元素是graphics类型,查询使用以下函数来更改 DOT 标记的颜色和形状。
declare function local:gfx($doc) {
for $layout-leafs in $doc//layout-leaf
let $graphics := $doc/realization//graphics
where $graphics[contains(@xref, $layout-leafs/@xref)]
return concat('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)
};
但是,这不会产生想要的结果,因为它"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];
也为第二个图(名为layout-root-two)返回该线,尽管它应该只出现在第一个图(名为layout-root)中。
我应该如何修改该函数,以便在layout-structure下的每个layout-root的情况下检查图形元素?
我尝试使用变量$root调用该函数,但这会导致静态错误。
想要的结果如下所示:
graph "layout-root" {
"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];
"layout-root" -- "header-text";
"header-text" -- "lay-1.01";
"header-text" -- "lay-1.02";
"layout-root" -- "lay-1.03";
}
graph "layout-root-two" {
"layout-root-two" -- "header-text-two";
"header-text-two" -- "lay-1.04";
"header-text-two" -- "lay-1.05";
}