如何使用XQuery函数来解析源XML的特定部分?

时间:2012-09-03 13:58:43

标签: xml function xquery

我需要根据以下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中的每个 layout-root 元素生成DOT标记:

declare variable $newline := '&#10;';
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";
}

如您所见,如果元素的类型为图形,则查询使用以下函数来更改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)
};

但是,这不会产生想要的结果,因为它也为第二个图形(名为 layout-root-two )返回行"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];,尽管它只应出现在第一个图(名为 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";
}

1 个答案:

答案 0 :(得分:1)

您需要realization,它不在$root的子树中。尝试仅传入graphics节点:

declare function local:gfx($root, $graphics) {
  for $layout-leafs in $root//layout-leaf
  where $graphics[contains(@xref, $layout-leafs/@xref)]
  return concat('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)
};

let $doc := doc("layout-data.xml")/layout,
    $graphics := $doc/realization//graphics
for $root in $doc/layout-structure/*
return string-join(('graph "', $root/@id, '" { ', $newline,
         local:gfx($root, $graphics),local:ref($root),'}', $newline), "")