在XQuery中调用concat函数中的多个函数

时间:2012-09-03 10:52:06

标签: xml function xquery

我需要从以下XML生成DOT图。

<layout>
<layout-structure>
    <layout-root id="layout-root" orientation="landscape">
        <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-leaf xref="lay-1.03"/>
    </layout-root>
</layout-structure>
<realization>
    <text xref="lay-1.01"/>
    <text xref="lay-1.02"/>
    <graphics xref="lay-1.03 lay-1.04"/>
</realization>
</layout>

我使用以下XQuery生成DOT标记:

declare variable $newline := '&#10;';

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)
};

let $doc := doc("layout-data.xml")/layout
let $root := $doc/layout-structure/*
return concat('graph "', $root/@id, '" { ', $newline, local:ref($root),'}')

上面的查询工作正常并生成以下图表:

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

结果如下所示:

现在,我想要做的是为DOT图中的每个元素分配一组属性,具体取决于它们在XML中实现元素下定义的属性,如下所示:

当然,这需要以下DOT标记:

graph "layout-root" {
"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];
"lay-1.04" [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";
"layout-root" -- "lay-1.04";
}

我写了两个额外的变量和函数来选择和编写所需的DOT标记:

declare variable $dotgraphics := '[shape="box", style="filled", color="#b3c6ed"]';

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($layout-leafs/@xref, ' ', $dotgraphics, ';', $newline)
};

我的问题是:如何将 local:gfx 函数包含在上面的工作XQuery脚本中?

如果我只是在 local:ref($ root)之前调用函数* local:gfx($ doc),如下所示,

return concat('graph "', $root/@id, '" { ', $newline, local:gfx($doc), $newline, local:ref($root),'}')

查询返回一个错误,即多个项目的序列不能作为 concat 函数的参数;怎么能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以使用fn:string-join($strings[, $separator])来代替字符串。如果您使用$newline作为第二个参数(默认为空字符串),它甚至会为您插入换行符:

string-join(('foo', 'bar', 'baz'), '&#10;')

产量

foo
bar
baz
相关问题